Reputation: 93
I make for each variable in my dataframe a histogram, lineplot and boxplot to assess the distribution of each variable and plot these graphs in one window.
For variable VARIABLE
my code looks like:
variable_name_string = "VARIABLE"
hist = qplot(VARIABLE, data = full_data_noNO, geom="histogram",
fill=I("lightblue"))+
theme_light()
avg_price = full_data_noNO %>%
group_by(Month, Country) %>%
dplyr::summarize(avg = mean(VARIABLE, na.rm =
TRUE))
#line graph for different countries over time
line = ggplot(data=avg_price, aes(x=anydate(Month), y=VARIABLE,
group=Country)) +
xlab("Date")+
ylab(variable_name_string)+
geom_line(aes(color=Country), size = 1)+
theme_light()
#boxplot over different years
avg_price2 = avg_price
avg_price2$Month = format(as.Date(anydate(avg_price$Month), "%Y-%m-%d"),
"%Y")
box = ggplot(avg_price2, aes(x = Month, y=VARIABLE, fill = Month)) +
geom_boxplot()+
xlab("Date")+
ylab(variable_name_string)+
guides(fill=FALSE)+
theme_light()
var_name = grid.text(variable_name_string, gp=gpar(fontsize=20))
#merge plot into one window
grid.arrange(var_name, hist, line, box, ncol=2)
This works fine for one variable, but now I want to do this for every variable in my dataframe and save the merged plot window for all variables. I have been looking for almost the entire day but I cannot find a solution. Can anyone help me?
Upvotes: 0
Views: 137
Reputation: 497
Without reproducible example it is hard to help, but you could try to wrap your plotting code in a function and use lapply
to repeatedly call the function for all your variables.
make_plots <- function (variable_string) {
var_quo <- rlang::sym(variable_string)
hist = qplot(!!var_quo, data = full_data_noNO, geom="histogram",
fill=I("lightblue"))+
theme_light()
avg_price = full_data_noNO %>%
group_by(Month, Country) %>%
dplyr::summarize(avg = mean(!!var_quo, na.rm =
TRUE))
#line graph for different countries over time
line = ggplot(data=avg_price, aes(x=anydate(Month), y=!!var_quo,
group=Country)) +
xlab("Date")+
ylab(variable_string)+
geom_line(aes(color=Country), size = 1)+
theme_light()
#boxplot over different years
avg_price2 = avg_price
avg_price2$Month = format(as.Date(anydate(avg_price$Month), "%Y-%m-%d"),
"%Y")
box = ggplot(avg_price2, aes(x = Month, y=!!var_quo, fill = Month)) +
geom_boxplot()+
xlab("Date")+
ylab(variable_string)+
guides(fill=FALSE)+
theme_light()
var_name = grid.text(!!var_quo, gp=gpar(fontsize=20))
#merge plot into one window
combined <- grid.arrange(var_name, hist, line, box, ncol=2)
# Save combined plot at VARIABLE_plots.pdf
ggsave(paste0(variable_string, "_plots.pdf"), combined)
combined
}
# Make sure to pass the variable names as character vector
plots <- lapply(c("VARIABLE1", "VARIABLE2"), make_plots)
# OR
plots <- lapply(colnames(full_data_noNO), make_plots)
# Plots can also be accessed and printed individually
print(plots[["VARIABLE1"]])
Upvotes: 1