2016-06-21 12 views
0

私はユーザーのヒートマップをプロットする関数を書いています。次の例では、性別ごとの成績の経時変化をプロットしています。ggplotへのユーザー入力名

ただし、これは特殊なケースです。 「性別」は「クラス」のような別の名前を持つことがあります。 私はユーザーに特定の名前を入力させ、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

ありがとうございました!もう1つの質問:2番目の関数では、Genderを含む 'sampledata $ Gender < - factor(sampledata $ Gender)'をまだ使用しています。私が 'sampledata $ y < - factor(sampledata $ y)'を入力してもうまくいきません。この部分を解決するには? – Yukun

+0

ええ、その部分を変更するのを忘れました。私はそれらをより一般的にするために両方のコードを編集しました。今はすべてがうまくいくはずです。 – thepule

+0

y軸のラベルを "new_var"と "y"の代わりに "Gender"にしたい場合はどうすればいいですか? (性別はユーザーの入力と一致し、それは他の名前でもあります) – Yukun

関連する問題