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