R等用变量的内容替换变量

时间:2017-12-12 08:55:42

标签: r

如何用内容/值替换变量?请参阅下面的示例代码。

###########SET my plotting variable##########################
    plot_var_y<-'var1'
    plot_var_x<-'var2'
####################EVALUATE THE FOLLOWING##################
        temp$plot_var_y
        # The above expression evaluates to null which is not what I want.temp$var1 is what I was looking for

    plot1<-ggplot(aes(y = plot_var_y, x = as.factor(plot_var_x), fill = no_of_heads), data =temp) +
      geom_boxplot()+stat_summary(fun.y=mean, geom="line",aes(group=1),colour=I("red"))+stat_summary(fun.y=mean, geom="point",shape=18,size=3)+xlab("position")

1 个答案:

答案 0 :(得分:1)

使用aes_string代替aes

尝试

 plot1<-ggplot(aes_string(y = plot_var_y, x = as.factor(plot_var_x), fill = no_of_heads), data =temp) +
      geom_boxplot()+stat_summary(fun.y=mean, geom="line",aes(group=1),colour=I("red"))+stat_summary(fun.y=mean, geom="point",shape=18,size=3)+xlab("position")

请参阅 How to use a variable to specify column name in ggplot