收到一个错误,即ggplot2_3.2.0每组不能绘制多个箱形图 [英] Getting an error that ggplot2_3.2.0 can't draw more than one boxplot per group
问题描述
我有要使用 R
的 ggplot
绘制的 xy
数据:
I have xy
data that I'd like to plot using R
's ggplot
:
library(dplyr)
library(ggplot2)
set.seed(1)
df <- data.frame(group = unlist(lapply(LETTERS[1:5],function(l) rep(l,5))),
x = rep(1:5,5),
y = rnorm(25,2,1),
y.se = runif(25,0,0.1)) %>%
dplyr::mutate(y.min = y-3*y.se,
y.low = y-y.se,
y.high = y+y.se,
y.max = y+3*y.se)
如您所见,虽然 df $ x
是一个点( integer
),但 df $ y
有一个相关的错误,我希望包括使用箱形图.
As you can see, while df$x
is a point (integer
), df$y
has an associated error, which I would like to include using a box plot.
所以我的目的是使用 y.min
, y.low通过
, x
坐标在 df
中绘制每一行. y
, y.high
和 y.max
来构建 boxplot
和<代码>颜色,然后按 group
填充.这就是说,我想将 df
中的每一行都沿着单独的 x轴
位置和 df $ group
>刻面,这样,首先出现 df $ group
A
的五个副本,然后在其右边 df $ group
B
的副本,依此类推.
So my purpose is to plot each row in df
by its x
coordinate, using y.min
, y.low
, y
, y.high
, and y.max
to construct a boxplot
, and color
and fill
it by group
. That means, that I'd like to have each row in df
, plotted as a box
along a separate x-axis
location and faceted
by df$group
, such that df$group
A
's five replicates appear first, then to their right df$group
B
's replicates, and so on.
直到我刚刚安装了最新的 ggplot2
软件包( ggplot2_3.2.0
)之前,该代码一直用于我的目的:
This code used to work for my purpose until I just installed the latest ggplot2
package (ggplot2_3.2.0
):
ggplot(df,aes(x=x,ymin=y.min,lower=y.low,middle=y,upper=y.high,ymax=y.max,col=group,fill=group))+
geom_boxplot(position=position_dodge(width=0),alpha=0.5,stat="identity")+
facet_grid(~group,scales="free_x")+scale_x_continuous(breaks = integerBreaks())
现在我收到此错误:
Error: Can't draw more than one boxplot per group. Did you forget aes(group = ...)?
有什么主意吗?
推荐答案
此代码一直用于我的目的,直到我刚安装了最新的ggplot2软件包(ggplot2_3.2.0)
This code used to work for my purpose until I just installed the latest ggplot2 package (ggplot2_3.2.0)
您是正确的:我刚刚遇到了一个类似的错误,该错误是我最近使用ggplot2 boxplots编写的代码,只是发现了与ggplot2最新更新有关的新错误.正如Marius所指出的那样,在 aes()
中指定 group
确实也为我解决了这个问题.但是我不明白他的回答的其余部分,因为它没有提供构面...
You are right: I just experienced a similar error with a code I recently wrote using ggplot2 boxplots, and just find out this new error related to ggplot2 latest update.
As Marius already pointed out, specifying the group
in the aes()
did solve the problem for me also.
However I don't understand the rest of his answer, as it is not providing the faceting...
这是 facet_grid()
的有效解决方案,您很接近:
Here is a working solution with facet_grid()
, you were close:
ggplot(df,aes(x=x,ymin=y.min,lower=y.low,middle=y,upper=y.high,ymax=y.max,col=group,fill=group, group=x))+
geom_boxplot(position=position_dodge(width=0),alpha=0.5,stat="identity")+
facet_grid(~group,scales="free_x")
这篇关于收到一个错误,即ggplot2_3.2.0每组不能绘制多个箱形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!