如何在 x 轴上添加一个代表 ggplot2 中所有观察值的因子水平?
How to add a factor level on the x-axis that represents all the observations in ggplot2?
创建 x 轴具有以下每个因子水平的图很容易:
df <- data.frame(value = rnorm(100), group = rep(1:3, length=100))
ggplot() + geom_boxplot(aes(factor(group), value), data=df)
我想在 x 轴上添加另一个因子水平,它使用来自整个样本(而不是仅来自一组)的数据。手动方法是 rbind
数据框自身,如下所示:
df2 <- rbind(df, df)
df2$group[100:200] <- "entire sample"
ggplot() + geom_boxplot(aes(factor(group), value), data=df2)
但是,有时我的数据框非常复杂,我想避免重复我的数据框。有没有更好的方法?
你可以这样做:
ggplot() + geom_boxplot(aes(factor(group), value), data=df) +
geom_boxplot(aes('entire sample', value), data=df)
你得到相同的结果
创建 x 轴具有以下每个因子水平的图很容易:
df <- data.frame(value = rnorm(100), group = rep(1:3, length=100))
ggplot() + geom_boxplot(aes(factor(group), value), data=df)
我想在 x 轴上添加另一个因子水平,它使用来自整个样本(而不是仅来自一组)的数据。手动方法是 rbind
数据框自身,如下所示:
df2 <- rbind(df, df)
df2$group[100:200] <- "entire sample"
ggplot() + geom_boxplot(aes(factor(group), value), data=df2)
但是,有时我的数据框非常复杂,我想避免重复我的数据框。有没有更好的方法?
你可以这样做:
ggplot() + geom_boxplot(aes(factor(group), value), data=df) +
geom_boxplot(aes('entire sample', value), data=df)
你得到相同的结果