0

I am trying to plot a line graph using ggplot, using a fuction i call trends. I want to have the option of choosing whatever column from my df when passing it into my function wrt to the fixed column options and choosen country. Below is the code...

trends <- function(country, col){
#filtering the specified country of interest and selecting fixed columns, plus optional column to go with them.
my_data =  df %>% dplyr::filter(Countries == 'country') %>% 
select(col = paste0('col'), day, month) 
my_plot = ggplot(my_data, aes(x=day,  y=col)) + geom_line() + facet_wrap(~month)
              print(my_plot)
            }
            trends('Cameroon',  col='CFR')

but i get this error...

Error: Can't subset columns that don't exist.
x Column `col` doesn't exist.

Pls help. I have tried other suggested response to similar scenarios but to no avail.

2 Answers2

1

Since you are using character inputs as function arguments you can use !! sym(col) where needed. However, I am not so sure, where the "additional, optional columns" fit in. Where in your plot should they be used. You would need to provide a better example of what you have in mind.

trends <- function(country, col){
  my_data =  df %>% dplyr::filter(Countries == country) %>% 
    select(!! sym(col), day, month) 
  my_plot = ggplot(my_data, aes(x=day,  y= !! sym(col))) +
    geom_line() +
    facet_wrap(~ month)
  print(my_plot)
}
TimTeaFan
  • 17,549
  • 4
  • 18
  • 39
  • Thank you so much @TimTeaFan, It worked. I am amazed that you managed to solve this without all the details in terms of what I had in mind. That's amazing!. If you do not mind me asking where can i learn more about this !!sym function. Plus the facet_grid for the month did not go chronologically like instead of Jan-Feb-Mar, i got Feb-Jan-Mar. Pls help again. Thanks before hand. – Dezzy7model Jun 21 '20 at 14:44
  • @Dezzy7model I think I read About `sym` in an older version of the "programming with dplyr" vgnette. Apparently, the vignette has been updated. Another good source is Hadley Wickhams book "Advanced R". Regarding the ordering of your `facet_grid` you can follow [this answer on SO](https://stackoverflow.com/a/14264107/9349302), it should work . – TimTeaFan Jun 23 '20 at 08:56
1

Personally I would prefer tidy eval. However, the main issue is that you put quotes around your vars country and col inside the body of your fun. Try this:

trends <- function(country, col){
    #filtering the specified country of interest and selecting fixed columns, plus     optional column to go with them.
    my_data =  df %>% dplyr::filter(Countries == country) %>% 
    select(col = col, day, month) 
    my_plot = ggplot(my_data, aes(x=day,  y=col)) + geom_line() + facet_wrap(~month)
    print(my_plot)
}
trends('Cameroon',  col='CFR')

Using the gapminder dataset as example dataset:

library(ggplot2)
library(dplyr)

df <- gapminder::gapminder %>% 
  rename(Countries = country)

trends <- function(country, col) {
  #filtering the specified country of interest and selecting fixed columns, plus optional column to go with them.
  my_data =  df %>% dplyr::filter(Countries == country) %>% 
    select(col = col, year, continent)#, day, month) 
  #my_plot = ggplot(my_data, aes(x=day,  y=col)) + geom_line() + facet_wrap(~month)
  my_plot = ggplot(my_data, aes(x=year,  y=col)) + geom_line() + facet_wrap(~continent)
  print(my_plot)
}

trends('Cameroon',  col='lifeExp')

stefan
  • 90,330
  • 6
  • 25
  • 51
  • Thank you so much stefan! It worked. Just that my months did not facet_grid chronologically like instead of Jan-Feb-Mar, i got Feb-Jan-Mar. Regardless, its manageable. – Dezzy7model Jun 21 '20 at 14:32