FcmC
FcmC

Reputation: 143

Looping over columns of data frame to create plots with ggplot2

I am trying to overcome this. Can't get any further.

I have a dataframe with factor and numeric variables. Herewith displayed are the first few rows and columns.

# A tibble: 6 x 5
  cluster SEV_D SEV_M   OBS   PAN  
    <int> <dbl> <dbl> <fct> <fct>  
1       1     5     1    0     1    
2       2     6     1    0     0    
3       1     5     1    0     1    
4       2     4     2    0     0    
5       1     4     1    1     1    
6       1     4     2    1     0   

cluster=as.factor(c(1,2,1,2,1,1))  
SEV_D=as.numeric(c(5,6,5,4,4,4))
SEV_M=as.numeric(c(1,1,1,2,1,2))
OBS=as.factor(c(0,0,0,0,1,1))
PAN=as.factor(c(1,0,1,0,1,0))
data<-data.frame(cluster,SEV_D,SEV_M,OBS,PAN) 

I split the dataframe like this, in numeric and factor variables, keeping 'cluster' in both subsets since I need it for grouping.

data_fact <- data[, sapply(data, class) == 'factor']

data_cont <- data[, sapply(data, class) == 'numeric' | names(data) 
== "cluster"]

The two following snippets of code would produce the plots I want.

data_fact %>% group_by(cluster,OBS)%>%summarise(total.count=n()) %>% 
ggplot(., aes(x=cluster, y=total.count, fill=OBS)) + 
geom_bar(position = 'dodge', stat='identity') + 
geom_text(aes(label=total.count),  
position=position_dodge(width=0.9), vjust=-0.2)

data_cont %>% group_by(cluster) %>% dplyr::summarise(mean = 
mean(SEV_D), sd = sd(SEV_D)) %>% 
ggplot(.,aes(x=cluster,y=mean))+geom_bar(position=position_dodge(), 
stat="identity",colour="black",size=.3)+geom_errorbar(aes(ymin=mean- 
sd, ymax=mean+sd),size=.3,width=.4,position=position_dodge(.4)) + 
ggtitle("SEV_D")

My goal is to create as many graphs as variables in the data frame, looping over columns and to store such graphs in one single sheet.

My attempt was

col<-names(data_fact)[!names(data_fact)%in%"cluster"]

for(i in col) {
data_fact %>% group_by(cluster,i)%>%summarise(total.count=n()) %>% 
ggplot(., aes(x=cluster, y=total.count, fill=i)) + geom_bar(position 
= 'dodge', stat='identity') + geom_text(aes(label=total.count), 
position=position_dodge(width=0.9), vjust=-0.2)
}

But it throws this error:

Error in grouped_df_impl(data, unname(vars), drop) : Column i is unknown

On top of that, that code would not display all graphs in one sheet I am afraid. Any help would be much appreciated!!!

Upvotes: 3

Views: 2392

Answers (1)

Dave2e
Dave2e

Reputation: 24139

The link above is a good reference. Or see Rstudio's tidyeval cheatsheet: https://github.com/rstudio/cheatsheets/raw/master/tidyeval.pdf

To evaluate i in the ggplot statement, you need to unquote the string with the !!ensym( ) function construct. Also, you will need to use the print statement to print the plots within the loop.

library(ggplot2)

col<-names(data_fact)[!names(data_fact)%in%"cluster"]

for(i in col) {
   print(i)

   g<-data_fact %>% group_by(cluster, !!ensym(i)) %>% summarise(total.count=n()) %>% 
     ggplot(., aes(x=cluster, y=total.count, fill=!!ensym(i))) + 
     geom_bar(position  = 'dodge', stat='identity') + 
     geom_text(aes(label=total.count), position = position_dodge(width=0.9), vjust=-0.2) +
     labs(title=i)
  print(g)
}

Upvotes: 3

Related Questions