0

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...

zx8754
  • 52,746
  • 12
  • 114
  • 209
Amazonian
  • 391
  • 2
  • 8
  • 22
  • Use [aes_string](https://stackoverflow.com/questions/28897577/what-is-the-difference-between-aes-and-aes-string-ggplot2-in-r) – zx8754 Nov 06 '18 at 08:49
  • Or use tidy evaluation approach https://stackoverflow.com/questions/50725950/scale-value-inside-of-aes-string/50726130#50726130 | https://stackoverflow.com/questions/50929344/multiple-plots-in-for-loop-ignoring-par/50930640#50930640 – Tung Nov 06 '18 at 17:37

0 Answers0