'Grouped Boxplot on discrete x-axis in R
I have a dataframe similar to the below:
df
variable n value
1 x_1 200 2.883524e+22
2 x_1 200 6.228677e+19
3 x_1 200 1.152941e+23
4 x_1 200 2.873297e+20
5 x_1 200 2.860104e+22
6 x_2 200 2.756752e+04
7 x_2 200 6.430455e+02
8 x_2 200 5.933116e+09
9 x_2 200 5.935287e+09
10 x_2 200 1.283504e+06
11 x_1 250 2.137800e+06
12 x_1 250 1.439365e+04
13 x_1 250 5.685357e+10
14 x_1 250 5.727160e+10
15 x_1 250 4.151983e+08
16 x_2 250 5.181344e+06
17 x_2 250 1.899797e+04
18 x_2 250 5.301975e+10
19 x_2 250 5.434231e+10
20 x_2 250 2.158825e+09
I am trying to develop a grouped box plot with n on the x-axis, value on the y-axis (using scale_y_log10()), and variable as the fill boxplots. However, I am struggling to actually plot each value of n on the x_axis and grouping the box plots. I currently have:
ggplot(data = df, mapping = aes(x = n, y = value, fill = variable)) +
geom_boxplot(aes(group = cut_width(variable, 0.25))) +
scale_y_log10()
but am struggling to get the results I was hoping for, is it possible to develop this type of box plot on a continuous x-axis and group by variable?
Thanks in advance.
Solution 1:[1]
You could make the x axis discrete, simply feeding in the extra factor levels that you want to make the appropriate breaks in the x axis:
ggplot(data = df, aes(x = factor(n, levels = c(200, 250, 300)), y = value)) +
geom_boxplot(aes(fill = variable)) +
scale_y_log10() +
scale_x_discrete(drop = FALSE, name = 'n')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Allan Cameron |

