I have some code to make plots for all of the variables in my data (36) and export them automatically. It works great, but the set binwidth is pretty small. When I try to change the bindwith, it makes it really small or really big for my graphs depending on their y-axis scale. How can I increase it proportionally for all?
# Plot separate ggplot figures in a loop.
library(ggplot2)
# Make list of variable names to loop over.
var_list = combn(names(LessCountS)[1:37], 2, simplify=FALSE)
my_comparisons <- list( c("HC", "IN"), c("IN", "OUT"), c("HC", "OUT") )
symnum.args <- list(
cutpoints = c(0.0001, 0.001, 0.01, 0.05, 1),
symbols = c("***", "**", "*", "NS")
)
# Make plots.
plot_list = list()
for (i in 1:37) {
p = ggplot(LessCount1, aes_string(x=var_list[[i]][1], y=var_list[[i]][2])) +
geom_dotplot(aes(fill= Type),
binaxis = "y", stackratio = .5, binwidth = 90,
stackdir = "center"
) +
theme_gray ()+
labs(x="", y = "Cell Count (cells/\u03bcL)") +
ggtitle(var_list[[i]][2]) +
scale_x_discrete(labels=c("HC" = "Controls", "IN" = "Inpatients",
"OUT" = "Outpatients")) +
theme(plot.title = element_text(hjust = 0.5, vjust = 2), legend.text=element_text(size=12),
axis.text = element_text(size=14),
axis.title = element_text(size = 14)) +
scale_fill_manual(values=c("#CCCCCC", "#990066", "#3366CC")) +
stat_summary(fun.y = median, fun.ymin = median, fun.ymax = median,
geom = "crossbar", width = 0.5, size = .45) +
stat_compare_means(comparisons = my_comparisons, label.y = , label = "p.signif", size = 5, symnum.args = symnum.args) +
stat_compare_means(label.y = )
plot_list[[i]] = p
}
# Save plots to tiff. Makes a separate file for each plot.
for (i in 1:37) {
file_name = paste("LessCount1_plot_", var_list[[i]][2], ".tiff", sep="")
tiff(file_name)
print(plot_list[[i]])
dev.off()
}
Examples of the outcome if I change the bindwidth
.
If I dont specify the binwidth
they are the same size, but quite small. I want to increase it proportionally for all irrespective of their scale, hoping this is possible!
Thanks in advance, S