0

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. enter image description here enter image description here

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

A Bedoya
  • 85
  • 9
  • Note that it is not quite easy to deal with your question since you don't provide data for a reproducible example and your code contains many parts that are irrelevant to the specific problem you have. Check out the links for a minimal, reproducible example (https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example, https://stackoverflow.com/help/mcve). – hplieninger May 24 '19 at 06:45
  • [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – hplieninger May 29 '19 at 07:35

1 Answers1

0

You need to make binwidth conditional on the data rather than setting it to a fixed value. Here's an example:

library("ggplot2")

for (ii in 1:5) {
    y <- names(mtcars)[ii]
    p <- ggplot(mtcars, aes(x = 1, y = !!sym(y))) +
        geom_dotplot(binaxis = "y", stackdir = "center",
                     # binwidth = 1
                     binwidth = diff(range(mtcars[, y]))/20)
    print(p)
}
hplieninger
  • 3,214
  • 27
  • 32