I am trying to graph with the following code
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)
The problem is when I am passing x, y, fill as strings, the function is not recognizing them as I would like them to; for example, I want x = get(groupingvar)
to be the same as x = country
.
I've tried get(), eval(), eval(parse), as.name(), do.call() but nothing seem to work...