0

I need to plot an area stack chart where the fill can be changed programmatically by changing a "string holder" variable. Here is an example of what I'd like to do.

this_group_label <- "Roots & Tubers"
#[...lots of code in which a data frame df_plot is created with a column named "Roots & Tubers...]"
gg <- ggplot(df_plot, aes(x = year, y = `Pctge CC`, fill = this_group_label))
gg <- gg + geom_area(position = "stack")
gg

So that I could then change the string stored in this_group_label when dealing with a new data frame with a different column name.

I have tried aes_string()

this_group_label <- "Roots & Tubers"
#[...lots of code in which a data frame df_plot is created with a column named "Roots & Tubers...]"
gg <- ggplot(df_plot, aes_string("year", "`Pctge CC`", fill = this_group_label))
gg <- gg + geom_area(position = "stack")
gg

and get()

this_group_label <- "Roots & Tubers"
#[...lots of code in which a data frame df_plot is created with a column named "Roots & Tubers...]"
gg <- ggplot(df_plot, aes(x = year, y = `Pctge CC`, fill = get(this_group_label)))
gg <- gg + geom_area(position = "stack")
gg

to no avail. When I try these last two I am getting the error Error in FUN(X[[i]], ...) : object 'Roots' not found

Cœur
  • 37,241
  • 25
  • 195
  • 267
ben
  • 787
  • 3
  • 16
  • 32
  • 2
    Don't use backticks in the `aes_string` version, just do `y = "Pctge CC"`. – Gregor Thomas Jun 26 '18 at 16:37
  • @Gregor Implemented your correction, still getting error `Error in FUN(X[[i]], ...) : object 'Roots' not found` – ben Jun 26 '18 at 16:40
  • 1
    See @hadley's comment in this issue: [aes_string fails with space in column name](https://github.com/tidyverse/ggplot2/issues/2447). Dupe-oid: [ggplot2 aes_string() fails to handle names starting with numbers or containing spaces](https://stackoverflow.com/questions/13445435/ggplot2-aes-string-fails-to-handle-names-starting-with-numbers-or-containing-s). [Adding space in the variable names with aes_string](https://stackoverflow.com/questions/26061184/adding-space-in-the-variable-names-with-aes-string) – Henrik Jun 26 '18 at 16:46
  • [Using ggplot2 with columns that have spaces in their names](https://stackoverflow.com/questions/29133567/using-ggplot2-with-columns-that-have-spaces-in-their-names) – Henrik Jun 26 '18 at 16:49
  • In addition: please post a _minimal reproducible example_. – Henrik Jun 26 '18 at 16:51
  • Possible duplicate of [Using ggplot2 with columns that have spaces in their names](https://stackoverflow.com/questions/29133567/using-ggplot2-with-columns-that-have-spaces-in-their-names) – divibisan Jun 26 '18 at 17:43

2 Answers2

0

This works:

this_group_label <- "Roots & Tubers"
#[...lots of code in which a data frame df_plot is created with a column named "Roots & Tubers...]"
fill_label <- paste0("`", this_group_label, "`")
gg <- ggplot(df_plot, aes_string("year", "`Pctge CC`", fill = fill_label))
gg <- gg + geom_area(position = "stack")
gg

Note the backticks around Pctge CC are also necessary for this to work.

ben
  • 787
  • 3
  • 16
  • 32
  • This will fail if your `fill_label` value contains spaces. To fix it, you need to use a little detour: `deparse(as.name(fill_label), backtick = TRUE)` (note that the naïve solution, putting backticks around the string, may also fail). – Konrad Rudolph Jun 26 '18 at 17:45
  • @KonradRudolph `this_group_label` has spaces, but then this is fixed by the admittedly naive and amateurish `paste0("`", this_group_label, "`")` trick. Given this trick, `fill_label` will never contain spaces. – ben Jun 26 '18 at 17:52
  • Right, I overlooked that. But as mentioned I think that could fail (not for simple spaces though). A more robust and more descriptive way is at any rate shown in my previous comment. – Konrad Rudolph Jun 26 '18 at 18:13
0

rlang::sym takes a string and converts it to a symbol. You just need to unquote it using !!, because aes expects unevaluated expressions:

library(rlang)

gg <- ggplot( df_plot, aes(x=year, y=`Pctge CC`, fill = !!sym(this_group_label)) )
gg <- gg + geom_area(position = "stack")
gg
Artem Sokolov
  • 13,196
  • 4
  • 43
  • 74