包含 geom_boxplot 中填充美学中使用的缺失因子水平的空间

问题描述 投票:0回答:4

我正在尝试在 R 中绘制一个盒须图。我的代码如下。目前,因为我在两个网站之一中只有两个月的数据,所以该网站的条形更宽(因为月份的第三个级别被删除)。

相反,我希望站点

A
具有与站点
B
相同的盒子图案(即右侧有一个空盒子的空间)。当我只有一个因素但似乎无法用“填充”因素做到这一点时,我可以轻松地使用
drop=TRUE
做到这一点。

Month=rep(c(rep(c("Jan","Feb"),2),"Mar"),10)
Site=rep(c(rep(c("A","B"),each=2),"B"),10)
factor(Month)
factor(Site)
set.seed(1114)
Height=rnorm(50)
Data=data.frame(Month,Site,Height)
plot = ggplot(Data, aes(Site, Height)) +
       geom_boxplot(aes(fill=Month, drop=TRUE), na.rm=FALSE)
plot
r ggplot2 boxplot
4个回答
24
投票

这是一个基于创建虚假数据的解决方案:

首先,将新行添加到数据框中。它包含不存在的因子水平组合的数据点(

Mar
A
)。
Height
的值必须超出真实
Height
数据的范围。

Data2 <- rbind(Data, data.frame(Month = "Mar", Site = "A", Height = 5))

然后就可以生成绘图了。由于假数据不应该是可见的,因此必须使用

coord_cartesian
和原始
Height
数据的范围修改 y 轴限制。

library(ggplot2)
ggplot(Data2, aes(Site, Height)) +
  geom_boxplot(aes(fill = Month)) +
  coord_cartesian(ylim = range(Data$Height) + c(-.25, .25))

enter image description here


14
投票

实现所需外观的一种方法是更改绘图时生成的数据。

首先,将绘图保存为对象,然后使用

ggplot_build()
将绘图数据的所有部分保存为对象。

p<-ggplot(Data, aes(Site, Height,fill=Month)) + geom_boxplot()
dd<-ggplot_build(p)

列表元素数据包含用于绘图的所有信息。

dd$data

[[1]]
     fill      ymin      lower     middle      upper      ymax  outliers notchupper notchlower    x PANEL
1 #F8766D -1.136265 -0.2639268  0.1978071  0.5318349 0.9815675            0.5954014 -0.1997872 0.75     1
2 #00BA38 -1.264659 -0.6113666  0.3190873  0.7915052 1.0778202            1.0200180 -0.3818434 1.00     1
3 #F8766D -1.329028 -0.4334205  0.3047065  1.0743448 1.5257798            1.0580462 -0.4486332 1.75     1
4 #00BA38 -1.137494 -0.7034188 -0.4466927 -0.1989093 0.1859752 -1.759846 -0.1946196 -0.6987658 2.00     1
5 #619CFF -2.344163 -1.2108919 -0.5457815  0.8047203 2.3773189            0.4612987 -1.5528617 2.25     1
  group weight ymin_final ymax_final  xmin  xmax
1     1      1  -1.136265  0.9815675 0.625 0.875
2     2      1  -1.264659  1.0778202 0.875 1.125
3     3      1  -1.329028  1.5257798 1.625 1.875
4     4      1  -1.759846  0.1859752 1.875 2.125
5     5      1  -2.344163  2.3773189 2.125 2.375

您对

x
xmax
xmin
值感兴趣。前两行对应级别
A
。这些值应该改变。

dd$data[[1]]$x[1:2]<-c(0.75,1)
dd$data[[1]]$xmax[1:2]<-c(0.875,1.125)
dd$data[[1]]$xmin[1:2]<-c(0.625,0.875)

现在使用

ggplot_gtable()
grid.draw()
绘制更改的数据。

library(grid)
grid.draw(ggplot_gtable(dd))

enter image description here


4
投票

现在有一种简单的方法可以在位置参见此处使用“保留”来执行此操作。对于上面的图,这将是:

Month = rep(c(rep(c("Jan", "Feb"), 2), "Mar"), 10)
Site = rep(c(rep(c("A", "B"), each = 2), "B"), 10)

factor(Month)
factor(Site)

set.seed(1114)

Height = rnorm(50)
Data = data.frame(Month, Site, Height)

plot = ggplot(Data, aes(Site, Height)) +
  geom_boxplot(
    aes(fill = Month, drop = TRUE),
    na.rm = FALSE,
    ## Note:
    position = position_dodge(preserve = 'single')
  )
plot


0
投票

两步:

  1. 使用“tidyr”中的“完整”为缺少的“月份”和“站点”组合创建虚假数据(例如,“max(Data$Height) * 1000”):

    图书馆(dplyr) 图书馆(tidyr)
    new_data <- Data %>%完成(月份,站点,填充=列表(高度= max(数据$高度)* 1000))

  2. plot without使用

    ylim
    中的
    coord_cartesian
    创建的假数据:

    ggplot(new_data, aes(场地, 高度)) + geom_boxplot(aes(fill = Month)) + coord_cartesian(ylim = 范围(数据$高度))

© www.soinside.com 2019 - 2024. All rights reserved.