0

I am having issue of ggplot in loop. It works well individually.

as below:

*plotgg<-
  ggplot(renewalplot, aes(x = Month,y=Rate)) +
    stat_summary(fun = mean, na.rm = TRUE,geom="bar")+
    labs(x = "Month") +
  ggtitle("Rate Change Distribution")+
  theme(axis.text.x = element_text(angle = 90, hjust = 1))+
  facet_grid(cols = vars(Year))
print(plotgg)*

when I put them in loop, it gives me error:

vars <- colnames(detailinfo_renewal_1)
varslist1 = vars[c(13)]


for (i in varslist1) {
  renewalplot <- detailinfo_renewal_1 %>%
    filter(Product=="FI") 
  
  plotgg<-
    ggplot(renewalplot, aes(x = renewalplot[, i],y=Rate)) +
    stat_summary(fun = mean, na.rm = TRUE,geom="bar")+
    labs(x = i) +
    ggtitle("Mean of Rate Change Distribution")+
    theme(axis.text.x = element_text(angle = 90, hjust = 1))+
    facet_grid(cols = vars(Year))
  print(plotgg)
  
  
}

Much appreciated it! LC

  • the error is "Don't know how to automatically pick scale for object of type tbl_df/tbl/data.frame. Defaulting to continuous. Error in is.finite(x) : default method not implemented for type 'list'" – user14189785 Jan 31 '21 at 00:46
  • Please add data using `dput` or something that we can copy and use. Read about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and [how to give a reproducible example](http://stackoverflow.com/questions/5963269). – Ronak Shah Jan 31 '21 at 02:17

2 Answers2

1

The bit that you've changed when putting into a loop tells you a lot about where the error might be:

aes(x = renewalplot[, i],y=Rate)

This method of mapping an aesthetic won't work. Normally, when you pick an aesthetic, you pass the name of the aesthetic to the value of x, such as in x = Month. Behind the scenes, ggplot() then figures out the appropriate values from your data source.

An alternative way of mapping aesthetics is to use aes_string(), which would probably be better suited to your use-case. Since i is already the name of the column as a string, this will fit right in

for (i in varslist1) {
  renewalplot <- detailinfo_renewal_1 %>%
    filter(Product=="FI") 
  
  plotgg<-
    ggplot(renewalplot, aes_string(x =  i, y = "Rate")) +
    stat_summary(fun = mean, na.rm = TRUE,geom="bar")+
    labs(x = i) +
    ggtitle("Mean of Rate Change Distribution")+
    theme(axis.text.x = element_text(angle = 90, hjust = 1))+
    facet_grid(cols = vars(Year))
  print(plotgg)
  
  
}
0

You can write a function :

library(dplyr)
library(ggplot2)

plot_fn <- function(col) {
  renewalplot <- detailinfo_renewal_1 %>% filter(Product=="FI") 
  
  ggplot(renewalplot, aes(x = .data[[col]],y=Rate)) +
    stat_summary(fun = mean, na.rm = TRUE,geom="bar")+
    labs(x = col) +
    ggtitle("Mean of Rate Change Distribution")+
    theme(axis.text.x = element_text(angle = 90, hjust = 1))+
    facet_grid(cols = vars(Year))
}

and use lapply to apply it for every value in varslist1.

list_plot <- lapply(varslist1, plot_fn)
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Thank you Ronak. awesome, it works! Actually, I only need to change the "aes(x = renewalplot[, i],y=Rate)" to "aes(x =.data[[i]],y=Rate)". Could i ask, when to use dataframe[,i] and when to use dataframe[[i]]? – user14189785 Jan 31 '21 at 04:07
  • `.data` is a special keyword which is used when you pass column name as string (like here). I can't remember any use case to use `dataframe[,i]` anytime. – Ronak Shah Jan 31 '21 at 04:11
  • HI Ronak, could I invite you for another question which is not raised by me, since you are very knowledgeable. thanks....https://stackoverflow.com/questions/59313161/loop-to-export-multiple-dataframes-in-r-to-excel-and-name-each-sheet – user14189785 Jan 31 '21 at 04:29