我正在写一个函数为用户绘制热图。在下面的例子中,它绘制了不同性别的随着时间的变化。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"))
}
我认为你需要'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