【发布时间】:2017-12-19 10:32:03
【问题描述】:
我一直对似乎很简单的事情有疑问:具有连续 x 轴的分组箱线图。
这里是最小数据数据:
df <- cbind(expand.grid(x=1:10, rep=1:20, fill=c("A", "B")), y=runif(400))
这就是我想要的;你会看到我已经强制 x 轴是离散的:
ggplot(df, aes(x=as.factor(x), y=y, fill=fill)) + geom_boxplot()
这是当我将x 保留为连续的,没有分组时得到的:
ggplot(df, aes(x=x, y=y, fill=fill)) + geom_boxplot()
当我添加分组时,颜色消失:
ggplot(df, aes(x=x, y=y, group=x, fill=fill)) + geom_boxplot()
要明确的是,我想要的 geom_point 是:
ggplot(df, aes(x=x, y=y, group=x, color=fill)) + geom_point(position=position_dodge(width=.7))
...但是如果我尝试在箱线图中设置闪避:
ggplot(df, aes(x=x, y=y, color=fill)) + geom_boxplot(position=position_dodge(width=.7))
有什么建议吗?我试过四处搜索:this question 解决了连续箱线图,但没有着色问题; this question 让我想知道是否需要设置交互,但似乎没有得到想要的结果。任何帮助将不胜感激!
【问题讨论】:
-
我知道这不是您所要求的,但您可以在
x上按间隔进行分组(取决于数据是否有效)。ggplot(df, aes(x=cut_interval(x=x, length=1), y=y, fill=fill)) + geom_boxplot()
标签: r plot ggplot2 grouping boxplot