I am trying to do 4 different plots with geom_density and put the count on my y-axis, then put them together with ggarrange. I am following this to be able to put count on my y-axis: Density plots with counts
Here is my data:
structure(list(group = c(1L, 2L, 3L, 4L, 1L, 2L, 4L, 1L, 2L,
3L, 4L, 1L, 2L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 4L, 1L, 2L, 3L, 4L,
1L, 2L, 3L, 4L, 1L, 2L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 4L, 1L, 2L,
3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 4L, 1L, 2L, 4L, 1L, 2L, 4L, 1L,
2L, 4L, 1L, 2L, 4L, 1L, 2L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 4L, 1L,
2L, 4L, 1L, 2L, 4L, 1L, 2L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 4L, 1L,
2L, 3L, 4L, 1L, 2L, 4L, 1L, 2L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 4L,
1L, 2L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 4L, 1L, 3L,
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 1L, 2L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L,
1L, 2L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L,
2L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), value = c(112.306866666667,
78.2724666666667, 0.339703333333333, 3.76666666666667, 146.513433333333,
162.5825, 6.93333333333333, 176.8, 175.4, 6.93333333333333, 6.06,
129.990666666667, 119.521666666667, 12.55, 161.6, 121.4, 0, 19.65,
172.337166666667, 184.2535, 27.7333333333333, 140.5034, 108.5384,
0.496946666666667, 12.35, 265, 184.2, 0, 55.8, 105.2671, 123.542766666667,
32.7166666666667, 194.4, 206, 0, 29.55, 144, 162.666666666667,
4.73333333333333, 134.333333333333, 113, 0.666666666666667, 5.6,
186, 193.666666666667, 1.33333333333333, 10, 261.333333333333,
184, 8.63333333333333, 140.666666666667, 110.333333333333, 2.06666666666667,
92, 158, 1.1, 70.6666666666667, 63.6666666666667, 2.73333333333333,
98.6666666666667, 77.6666666666667, 42.4666666666667, 102.666666666667,
80.3333333333333, 5.83333333333333, 108, 173.666666666667, 0,
984.083333333333, 186.666666666667, 150, 7.75, 140, 138.666666666667,
3.31666666666667, 121, 79.6666666666667, 4.41666666666667, 171.4405,
160.749233333333, 21.65, 180, 120, 0, 17.1, 162.867666666667,
128.443833333333, 24.6833333333333, 172, 114, 0.6, 18.65, 81,
72.3333333333333, 24.6, 143.666666666667, 70.6666666666667, 4.51666666666667,
138.333333333333, 153.666666666667, 12, 0.05, 195, 163, 0.666666666666667,
10.7666666666667, 76.6666666666667, 82, 0.333333333333333, 49.2833333333333,
92, 53, 0.333333333333333, 30.8666666666667, 62, 38, 0, 3.06666666666667,
102, 114.666666666667, 3.43333333333333, 42.6666666666667, 40,
0.466666666666667, 107.333333333333, 224.666666666667, 0, 28.3666666666667,
132, 142, 0, 16.0833333333333, 142, 146, 17.4333333333333, 78.6666666666667,
0.333333333333333, 11.6333333333333, 59.3333333333333, 99, 0.666666666666667,
0, 75, 67, 3.66666666666667, 57.7833333333333, 112, 74.6, 0.6,
98.92, 116, 72.3333333333333, 0, 17.7666666666667, 134.8, 76.6,
0, 25.38, 115.666666666667, 138.666666666667, 6.5, 125.4454,
71.9361, 0.04383, 29.4751166666667, 109.333333333333, 133, 0,
42.4079666666667, 137.810433333333, 112.988866666667, 8.405845,
36.6666666666667, 41.3333333333333, 0, 27.9833333333333, 32.44961,
35.7635666666667, 0.0935366666666667, 7.01737, 163.333333333333,
170.666666666667, 1.66666666666667, 21.7884666666667, 171, 161.333333333333,
6.71666666666667, 119.333333333333, 79.6666666666667, 7, 1.8,
148.666666666667, 238, 0.333333333333333, 17.95, 1, 50.3333333333333,
0, 0), type = structure(c(2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L,
2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), levels = c("Control",
"Case"), class = "factor")), row.names = c(NA, 199L), class = "data.frame")
It seems that only one of the plots gets the correct count (group 3), the rest have values under 1. I figured out that this has to do with the value of the bandwith of histograms after doing some searching. Because if I multiply with the default value 30
geom_density(aes(y=after_stat(count)*30)
I get the “correct” answer on the 3 plots where I get my y-axis under 1. Now my question is: Where do I extract that bandwidth constant where I multiply the count with so I can automate my plots? I am doing for loops for 5 other datasets as well and I want everything automated. My code is the following
plot_list <- list()
for(j in 1:length(unique(data1$group))){
tmp_data <- data1[data1$group == j,]
plot_list[[j]] <- ggplot(tmp_data, aes(x = value, fill = type)) +
geom_density(aes(y=after_stat(count)), alpha = 0.6) +
ylab("Frequency") + theme_minimal()
}