为什么Geom_Rect颜色仅facet_wrap的第一行?
我试图在我的 facet_wrap
绘图的每个偶数面板上获得阴影矩形。但是,当我使用 geom_Rect
时,它仅在第二个面板上产生矩形。我尝试使用 Annotate
和 geom_tile
,但没有成功。我认为我在这里缺少一些简单的细节,这可能与我的X变量是分类而不是数字有关的事实,但是我已经在几个小时进行了战斗...
这是我的代码:
even_numbers <- seq(2,nrow(df.plt),2)
ggplot(df.plt) +
geom_rect(data = df.plt[even_numbers, ],
xmin = even_numbers - 0.5, xmax = even_numbers + 0.5,
ymin = -Inf, ymax = Inf, alpha = 0.3, fill = 'grey') +
geom_boxplot(aes(x = Cnd, y = nst, fill = Srs), position = position_dodge(0.9), outlier.shape = 1) +
facet_wrap(vars(Grp), ncol=1)
以及由此产生的图: 带有geom_rect和facet_wrap无法按预期工作的
绘图
结果 复制我的问题:
set.seed(002) # just to make it reproducible
df.tmp = data.frame(nst = rnorm(100*2), Srs = sample(rep(c("S3","S4"),100)), Cnd = sample(rep(c("DN","DA","DV","DAV"),50)), Grp = sample(rep(c("close","far"),100)))
even_numbers <- seq(2,nrow(df.tmp),2)
ggplot(df.tmp) +
geom_rect(data = df.tmp[even_numbers, ],
xmin = even_numbers - 0.5, xmax = even_numbers + 0.5,
ymin = -Inf, ymax = Inf, alpha = 0.3, fill = 'grey') +
geom_boxplot(aes(x = Cnd, y = nst, fill = Srs), position = position_dodge(0.9), outlier.shape = 1) +
facet_wrap(vars(Grp), ncol=1)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尽管您的想法是正确的恕我直言,但您可以通过将
Xmin
和Xmax
值放在数据框架中并通过审美绘制美观来更轻松地实现所需的结果。首先要注意,我们只需要一个均匀数的长度的向量(unique(df.tmp $ cnd))
,即cnd
的类别数。其次,当我们混合离散和连续的X变量时,我在GEOM_RECT
之前添加了scale_x_discrete
,否则我们会遇到错误。edit 以防万一。您的方法不起作用的原因是,用于RECT的数据的相关部分仅包含
far
组。问题是,基本上仅显示与范围1到4(cnd
类别的数量)中对应的均匀数字。从以下代码段中可以看出,该代码段复制了用于方法中的Rects下的数据下的数据,仅存在far
grp(在过滤范围1至4的均匀数字之后):While your idea was right IMHO you could achieve your desired result more easily by putting the
xmin
andxmax
values in a dataframe and by mapping on aesthetics. First note that we only need a vector of even numbers oflength(unique(df.tmp$Cnd))
, i.e. the number of categories ofCnd
. Second, as we are mixing discrete and continuous x variables I added anscale_x_discrete
beforegeom_rect
as otherwise we will get an error.EDIT Just in case. The reason why your approach did not work is that the relevant part of the data used for the rects contains only the
far
group. The issue is that basically only rects corresponding to even numbers in the range 1 to 4 (the number ofCnd
categories) are displayed. As can be seen from the following code snippet which replicates the data which under the hood is used for the rects in your approach only thefar
grp is present (after filtering for even numbers in the range 1 to 4):我一直在尝试了解为什么只有第二部分被阴影。最终,我准备了第二个示例,其中我找到了另一个可能的解决方案。但是,我不完全满意,因为我不了解
geom_Rect
/facet_wrap
;我只找到了一些解决方法。这是一个示例:
正如您在此处看到的那样,尽管确保
df.tmp [vest_numbers,]
Incluber CLOSE> CLOSE datapoints,但该图仅在第二行中阴影矩形。 :在这里我更改
ggplot
,因此它包含geom_Rect
分别用于关闭
和far
segments:如下所示,它现在有效:
正如我之前提到的,我仍然不确定为什么
geom_rect
在第一个 地方。在我的解决方案中,需要为每个段准备一个单独的geom_Rect
,因此绝对不是其中许多图的解决方案。我试图找到一种更优雅的方式,因此不必打扰宣布多少个部分或其他分组。I have been trying to understand why only the second segment gets shaded. Eventually I prepared a second example, in which I have found another possible solution. However, I am not fully satisfied as I did not understand the issue with
geom_rect
/facet_wrap
; I only found some workaround.Here's the example:
As you can see here, the plot has shaded rectangles only in the second row, despite ensuring the
df.tmp[even_numbers, ]
includesclose
datapoints as well:Here I change the
ggplot
so it containsgeom_rect
separately forclose
andfar
segments:As you can see below, it works now:
As I mentioned earlier, I am still not sure why
geom_rect
did not work in the first place. In my solution, a separategeom_rect
needs to be prepared for each segment, so it's definitely not a solution for a plot with many of them. I was trying to find a more elegant way, so one wouldn't have to bother how many segments or other groupings are declared.