在geom_boxplot中包含用于填充美学的缺失因子级别的空间 [英] Include space for missing factor level used in fill aesthetics in geom_boxplot

查看:114
本文介绍了在geom_boxplot中包含用于填充美学的缺失因子级别的空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在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)
因子(月)
因子b $ b 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


解决方案

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

首先,将plot保存为对象,然后使用 ggplot_build()将绘图数据的所有部分保存为对象。


$ b

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

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

  dd $ data 

[[1]]
fill ymin lower middle upper ymax异常值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
组重量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] 0.625,0.875)

现在使用 ggplot_gtable() grid.draw()来绘制已更改的数据。

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


I am trying to draw a box and whisker plot in R. My code is below. At the moment, because I only have data for two months in one of the two sites, the bars are wider for that site (because the third level of month is dropped).

Instead, I would like the same pattern of boxes for site A as there is for site B (i.e. with space for an empty box on the right-hand side). I can easily do this with drop=TRUE when I only have one factor but do not seem to be able to do it with the "filling" factor.

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

解决方案

One way to achieve the desired look is to change data produced while plotting.

First, save plot as object and then use ggplot_build() to save all parts of plot data as object.

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

List element data contains all information used for plotting.

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

You are interested in x, xmax and xmin values. First two rows correspond to level A. Those values should be changed.

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)

Now use ggplot_gtable() and grid.draw() to plot changed data.

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

这篇关于在geom_boxplot中包含用于填充美学的缺失因子级别的空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆