且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用索引而不是名称指定列

更新时间:2023-11-26 22:01:40

@baptiste指出,您应该使用 aes_string()而不是 aes()来使用定义x和y值的字符串.另外,您还应该在引号中放入 value variable .

As pointed out by @baptiste you should use aes_string() instead of aes() to use strings in defining x and y values. Also you should put value and variable inside quotes.

PropBarPlot<-function(df, mytitle=""){
  melteddf<-melt(df, id=names(df)[1], na.rm=T)
  ggplot(melteddf, aes_string(x=names(df)[1],y= "value", fill="variable")) + 
    geom_bar(position="fill") + 
    theme(axis.text.x = element_text(angle=90, vjust=1)) + 
    labs(title=mytitle)
}