What I am trying to do is to tell ggplot to geom_line a column of a tibble that is quoted with ``.
For instance, if I write literally the "name" of the column it works:
this is the generated tibble:
Q <- as_tibble(data.frame(series = rep(c("diax","diay"),3),
value = c(3.25,3.30,3.31,3.36,3.38,3.42),
year = c(2018,2018,2019,2019,2020,2020))) %>%
select(year, series, value) %>% spread(key = "series", value = "value") %>%
rename(`2018-01-01` = diax, `2017-01-01` = diay)
And this is the ggplot command:
ggplot(Q, aes(x = year)) +
geom_line(aes(y = `2018-01-01`), col = "red", size = 2, linetype = "dotdash") +
geom_line(aes(y = `2017-01-01`), col = "orange", size = 2, linetype = "dashed")
The code above works just fine.
But if I have a vector of strings with the name of the columns, I simply cannot replicate the previous result calling the vector.
That is, suppose I have a vector like this:
nomes <- c("2018-01-01","2017-01-01")
and then I would like ggplot something like this:
ggplot(Q, aes(x = year)) +
geom_line(aes(y = nomes[1]), col = "red", size = 2, linetype = "dotdash") +
geom_line(aes(y = nomes[2]), col = "orange", size = 2, linetype = "dashed")
I know that this wouldn´t work, but as a beginner that I am, I would guess that the lines below would work just fine, but they don´t
ggplot(Q, aes(x = year)) +
geom_line(aes(y = !!quo(nomes[1])), col = "red", size = 2, linetype = "dotdash") +
geom_line(aes(y = !!quo(nomes[2])), col = "orange", size = 2, linetype = "dashed")
I realized that quo(nomes[1]) does not deliver the name inside the position of the vector, and I could not get what I wanted trying some alternatives I though about.