2016-06-21 60 views
0

我正在写一个函数为用户绘制热图。在下面的例子中,它绘制了不同性别的随着时间的变化。ggplot的用户输入名称

但是,这是一个特例。 “性别”可能有其他名称,如“Class”。 我会让用户输入他们的具体名称,然后让ggplot为每个轴标记正确的标签。

如何根据需要修改我的功能“heatmap()”?

sampledata <- matrix(c(1:60,1:60,rep(0:1,each=60),sample(1:3,120,replace = T)),ncol=3) 
colnames(sampledata) <- c("Time","Gender","Grade") 
sampledata <- data.frame(sampledata) 
heatmap=function(sampledata,Gender) 
{ 
sampledata$Time <- factor(sampledata$Time) 
sampledata$Grade <- factor(sampledata$Grade) 
sampledata$Gender <- factor(sampledata$Gender) 
color_palette <- colorRampPalette(c("#31a354","#2c7fb8", "#fcbfb8","#f03b20"))(length((levels(factor(sampledata$Grade))))) 
ggplot(data = sampledata) + geom_tile(aes(x = Time, y = Gender, fill = Grade))+scale_x_discrete(breaks = c("10","20","30","40","50"))+scale_fill_manual(values =color_palette,labels=c("0-1","1-2","2-3","3-4","4-5","5-6",">6"))+ theme_bw()+scale_y_discrete(labels=c("Female","Male")) 
} 
+1

我认为你需要'aes_string'。看看[this](https://nsaunders.wordpress.com/2013/02/26/rggplot2-tip-aes_string/)和[this](http://stats.stackexchange.com/questions/177129/ ggplot-and-loops) – Tung

回答

2

最简单的解决方案是使用aes_string重新定义函数。 当函数被调用时,你需要传递你想用作字符串的列 的名字。

heatmap=function(sampledata,y) 
{ 
     sampledata$Time <- factor(sampledata$Time) 
     sampledata$Grade <- factor(sampledata$Grade) 
     sampledata$new_var <- factor(sampledata[,y]) 
     color_palette <- colorRampPalette(c("#31a354","#2c7fb8", "#fcbfb8","#f03b20"))(length((levels(factor(sampledata$Grade))))) 
     ggplot(data = sampledata) + geom_tile(aes_string(x = "Time", y = "new_var", fill = "Grade"))+scale_x_discrete(breaks = c("10","20","30","40","50"))+scale_fill_manual(values =color_palette,labels=c("0-1","1-2","2-3","3-4","4-5","5-6",">6"))+ theme_bw()+scale_y_discrete(labels=c("Female","Male")) + ylab(y) 
} 


# Below an example of how you call the newly defined function 
heatmap(sampledata, "Gender") 

另外,如果你想保留自由报价的语法,有一个稍微复杂的解决方案:

heatmap=function(sampledata,y) 
{ 
     arguments <- as.list(match.call()) 
     axis_label <- deparse(substitute(y)) 
     y = eval(arguments$y, sampledata) 

     sampledata$Time <- factor(sampledata$Time) 
     sampledata$Grade <- factor(sampledata$Grade) 
     sampledata$y <- factor(y) 
     color_palette <- colorRampPalette(c("#31a354","#2c7fb8", "#fcbfb8","#f03b20"))(length((levels(factor(sampledata$Grade))))) 
     ggplot(data = sampledata) + geom_tile(aes(x = Time, y = y, fill = Grade))+scale_x_discrete(breaks = c("10","20","30","40","50"))+scale_fill_manual(values =color_palette,labels=c("0-1","1-2","2-3","3-4","4-5","5-6",">6"))+ theme_bw()+scale_y_discrete(labels=c("Female","Male")) + ylab(axis_label) 
} 

# Below an example of how you call the newly defined function 
heatmap(sampledata, Gender) 
+0

非常感谢!还有一个问题:在第二个函数中,我们仍然使用包含性别的'sampledata $ Gender < - factor(sampledata $ Gender)'。如果我把'samplesata $ y < - factor(sampledata $ y)',它就不起作用。如何解决这部分? – Yukun

+0

是的,我忘了改变那部分。我编辑了两个代码,以使它们更通用。现在一切都应该工作。 – thepule

+0

如果我想让y轴的标签为“性别”而不是“new_var”和“y”,该怎么办? (性别与用户输入一致,也可以是其他名称) – Yukun

相关问题