我正在尝试使用以下代码进行绘制
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(),但似乎没有任何作用...