用相关列重建给定数据框的 ggplot2 箱线图
Reconstruct ggplot2 boxplots given dataframe with relevant columns
我想知道在给定每个箱线图的预先计算值的情况下构建 ggplot2 箱线图的惯用方法是什么
> df
base p10 p90 lower_quartile mean median upper_quartile
1 1 32 35 33 33.63740 34 34
2 2 32 35 33 33.77753 34 35
3 3 32 36 33 33.89361 34 35
4 4 33 36 33 33.89691 34 35
5 5 32 35 33 33.85145 34 35
6 6 35 37 37 36.48259 37 37
正在尝试用
绘制这些图
ggplot(df, aes(base)) +
geom_boxplot(aes(ymin = p10,
lower = lower_quartile,
middle = median,
upper = upper_quartile,
ymax = p90),
stat = "identity")
没有给出想要的情节。我错过了什么?
我不知道你的 data.frame 中的底代表什么,但为了正确地做到这一点,你的 x 轴应该是离散的(以显示不同的箱线图)。然后对于 y 轴,您需要一个 ymin
、一个 lower
、一个 middle
、一个 upper
和一个您提供的 ymax
。 x 轴是用于绘制不同箱线图的变量。所以,如果你把它变成一个因素,那么它就起作用了:
library(ggplot2)
#I have added base as factor
ggplot(df, aes(factor(base))) +
geom_boxplot(aes(ymin = p10,
lower = lower_quartile,
middle = median,
upper = upper_quartile,
ymax = p90),
stat = "identity")
输出:
这样就可以了。
我想知道在给定每个箱线图的预先计算值的情况下构建 ggplot2 箱线图的惯用方法是什么
> df
base p10 p90 lower_quartile mean median upper_quartile
1 1 32 35 33 33.63740 34 34
2 2 32 35 33 33.77753 34 35
3 3 32 36 33 33.89361 34 35
4 4 33 36 33 33.89691 34 35
5 5 32 35 33 33.85145 34 35
6 6 35 37 37 36.48259 37 37
正在尝试用
绘制这些图ggplot(df, aes(base)) +
geom_boxplot(aes(ymin = p10,
lower = lower_quartile,
middle = median,
upper = upper_quartile,
ymax = p90),
stat = "identity")
没有给出想要的情节。我错过了什么?
我不知道你的 data.frame 中的底代表什么,但为了正确地做到这一点,你的 x 轴应该是离散的(以显示不同的箱线图)。然后对于 y 轴,您需要一个 ymin
、一个 lower
、一个 middle
、一个 upper
和一个您提供的 ymax
。 x 轴是用于绘制不同箱线图的变量。所以,如果你把它变成一个因素,那么它就起作用了:
library(ggplot2)
#I have added base as factor
ggplot(df, aes(factor(base))) +
geom_boxplot(aes(ymin = p10,
lower = lower_quartile,
middle = median,
upper = upper_quartile,
ymax = p90),
stat = "identity")
输出:
这样就可以了。