2016-06-21 95 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

相關問題