0

I am a noob, so hope this makes sense...

Question/problem statement

I need to create a number of plots, where the only difference in each of the plots is the group used - each group contains categorical variables. I have got this to work by manually typing out all of the code.

Instead of manually writing each of the groups into R, I want to develop a loop to automate this plotting process.

Current manual method

This works, but is tedious and I want to automate through a loop - just an example with 2 of my 9 groups.

The only thing that changes in each is the factor and titles

# GOR

ggplot(aes(y = dailyCV, x = factor(GOR)), data = mergedbed) +
  geom_jitter(alpha=1/2, color="tomato", position=position_jitter(width=.2), size=1/10) +
  stat_summary(fun.data = min.mean.sd.max, geom = "boxplot", alpha = 0.5) +
  stat_summary(fun.y=mean, colour="black", geom="text", 
               vjust=0.5, hjust=1.5, size=3, aes( label=round(..y.., digits=1))) +
  stat_summary(fun.data = give.n, geom = "text", vjust=1, hjust=-2, size=3) +
  coord_flip() +
  stat_summary(fun.y = mean, geom="point",colour="darkred", size=2) +
  xlab("GOR")+
  ylab("Co-efficient of variation (%)")+
  ggtitle("GOR vs dailyCV")

# ACCOM_EHCS

ggplot(aes(y = dailyCV, x = factor(ACCOM_EHCS)), data = mergedbed) +
  geom_jitter(alpha=1/2, color="tomato", position=position_jitter(width=.2), size=1/10) +
  stat_summary(fun.data = min.mean.sd.max, geom = "boxplot", alpha = 0.5) +
  stat_summary(fun.y=mean, colour="black", geom="text", 
               vjust=0.5, hjust=1.5, size=3, aes( label=round(..y.., digits=1))) +
  stat_summary(fun.data = give.n, geom = "text", vjust=1, hjust=-2, size=3) +
  coord_flip() +
  stat_summary(fun.y = mean, geom="point",colour="darkred", size=2) +
  xlab("ACCOM_EHCS")+
  ylab("Co-efficient of variation (%)")+
  ggtitle("ACCOM_EHCS vs dailyCV")

My attempt

My attempt here was to create a vector with each of the groups and then try to loop this, but it doesnt work and Im sure its very wrong. My first time at attempting to create a loop.

myvariables <- c("GOR","ACCOM_EHCS","DBL_GLAZ", "BUILDING_AGE", "HhdSize", "Inc_Group_7s", "Person_Under_5", "Person_Over_64", "thermal")

lapply(myvariables, function(cc){
  p <- ggplot(aes(y = dailyCV, x = factor(aes_string(y = cc))), data = mergedbed) +
  geom_jitter(alpha=1/2, color="tomato", position=position_jitter(width=.2), size=1/10) +
  stat_summary(fun.data = min.mean.sd.max, geom = "boxplot", alpha = 0.5) +
  stat_summary(fun.y=mean, colour="black", geom="text", 
               vjust=0.5, hjust=1.5, size=3, aes( label=round(..y.., digits=1))) +
  stat_summary(fun.data = give.n, geom = "text", vjust=1, hjust=-2, size=3) +
  coord_flip() +
  stat_summary(fun.y = mean, geom="point",colour="darkred", size=2) +
  xlab("???")+
  ylab("Co-efficient of variation (%)")+
  ggtitle("??? vs dailyCV")
    p
})

Thank you in advance

  • you can also make a plot `g <- ggplot(...) + ...` and then change the aesthetics - `g + aes(x = factor(ACCOM_EHCS)) + xlab(...)` – Richard Telford Dec 05 '18 at 18:50
  • These might help https://stackoverflow.com/a/49943976/& https://stackoverflow.com/a/50930640/& https://stackoverflow.com/a/50522928/ – Tung Dec 05 '18 at 19:05
  • If it's ok to produce one plot object (with multiple facets within), this might be more straightforward to do if you reshape your data into long format, and then use `facet_wrap(~GROUP_NAME, scales = "free)`. – Jon Spring Dec 05 '18 at 19:17
  • Take a look at use `aes_string` instead of `aes`. You will need to convert your columns to factors prior to the ggplot calls. – Dave2e Dec 05 '18 at 23:51

1 Answers1

0

Here is an example using the iris dataset and purrr:

library(tidyverse)
data(iris)

## create a grid with variable combinations
variables <- iris %>%
  select(everything(), -Species) %>%
  names() %>%
  expand.grid(x = ., y =., stringsAsFactors = F)

##create plotting function
plot_data <- function(data, x, y){
  ggplot(data, aes_string(x, y)) +
           geom_point() +
    ggtitle(paste(x, "vs", y))
}

map2(.x = variables$x,
     .y = variables$y,
     .f = ~ plot_data(iris, .x, .y))

This creates all variable combinations of plots and changes the title.

mpschramm
  • 520
  • 6
  • 12