当将变量名作为字符串输入时,R计算变量

时间:2018-11-06 08:42:26

标签: r string ggplot2 eval

我正在尝试使用以下代码进行绘制

myData = segmentation_country #data.frame with pct_country, contry, consumer_segmentation as variables
valuevar = paste("pct_country", sep='')
groupingvar = paste("factor(country)", sep='')
categoriesvar = paste("factor(consumer_segmentation)", sep='')
xlabel = "country"
ylabel = "% of cs in country"
graphtitle = "distribution of cs by country"
legendtitle = "consumer_segmentation"
p <- ggplot(data = myData, aes(x = get(groupingvar), y = get(valuevar),
                               fill = get(categoriesvar)))
p + geom_bar(stat = "identity",
             position = position_dodge(0.9)) +
  labs(x = xlabel, y = ylabel) +
  ggtitle(graphtitle) +
  scale_fill_discrete(name = legendtitle) +
  geom_text(aes(label=round(get(valuevar),1)), vjust=-0.6, color="black",
            position = position_dodge(0.9), size=3.5)

问题是当我将x,y填充为字符串时,函数无法按照我的意愿识别它们;例如,我希望x = get(groupingvar)x = country相同。

我已经尝试过get(),eval(),eval(parse),as.name(),do.call(),但似乎没有任何作用...

0 个答案:

没有答案