简体   繁体   English

ggplot2:带有facet_grid和自由缩放的boxplot

[英]ggplot2: boxplot with facet_grid and free scale

I am trying to have free scales on a Boxplot image with faceting. 我试图在带有刻面的Boxplot图像上有自由比例。

Using this example dataset, if I try this: 使用此示例数据集,如果我尝试这样做:

ggplot(data=mpg) +
geom_boxplot(aes(x=cty, y=model))+
facet_grid(manufacturer ~ drv, scales = "free", space = "free")

Plot incorrect boxplot http://dl.dropbox.com/u/9788680/plot1.png 绘制不正确的箱图http://dl.dropbox.com/u/9788680/plot1.png

Here, the free scales are implemented exactly as I would like, with the different scales for the y-axis depending on the number of available factors for a horizontal facet rule. 在这里,自由尺度完全按照我的意愿实现,y轴的不同尺度取决于水平面规则的可用因子的数量。 The boxplots are however not correctly depicted (ie as solid lines instead of boxplots). 然而,箱图未被正确描绘(即,实线而不是箱线图)。 When searching for a solution, I found that I should use coord_flip() in order to make the boxplot be depicted correctly, ie 在搜索解决方案时,我发现我应该使用coord_flip()以便正确描绘箱形图,即

ggplot(data=mpg) +
geom_boxplot(aes(x=model,y=cty))+
facet_grid(manufacturer ~ drv, scales = "free", space = "free")+
coord_flip()

Plot correct boxplot, but no scaling http://dl.dropbox.com/u/9788680/plot2.png 绘制正确的boxplot,但没有缩放http://dl.dropbox.com/u/9788680/plot2.png

In above picture, the boxplots are now correct. 在上图中,箱图现在是正确的。 However, the free scale for the factors (so on the y-axis) are removed. 但是,去除了因子(因此在y轴上)的自由标度。 Now, for each horizontal facet line, ALL the available factors across the dataset are now included, instead of only the factors available for each facet (as in Figure 1). 现在,对于每个水平构面线,现在包括数据集中的所有可用因子,而不是仅包含每个构面可用的因子(如图1所示)。

I would like to know how I can get the correct facetting with a free scale on both axes, correctly depicting the boxplot. 我想知道如何通过两个轴上的自由刻度获得正确的刻面,正确描绘了箱线图。

If somebody could point me in the right direction, I would be grateful. 如果有人能指出我正确的方向,我将不胜感激。

Thanks. 谢谢。

The desired behavior is supported at least as of ggplot2 2.2.1. 至少从ggplot2 2.2.1开始支持所需的行为。


library(ggplot2)
ggplot(data=mpg[which(mpg$manufacturer %in% c('audi', 'hyundai', 'jeep')),]) +
  geom_boxplot(aes(x=model,y=cty)) +
  facet_grid(manufacturer ~ drv, scales = "free", space = "free") +
  coord_flip()

sessionInfo()
#> R version 3.3.2 (2016-10-31)
#> Platform: x86_64-apple-darwin13.4.0 (64-bit)
#> Running under: OS X El Capitan 10.11.6
#> 
#> locale:
#> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] ggplot2_2.2.1
#> 
#> loaded via a namespace (and not attached):
#>  [1] Rcpp_0.12.11         digest_0.6.12        rprojroot_1.2       
#>  [4] plyr_1.8.4           grid_3.3.2           gtable_0.2.0        
#>  [7] backports_1.0.5      magrittr_1.5         evaluate_0.10.1     
#> [10] scales_0.4.1.9002    rlang_0.1.1.9000     stringi_1.1.5       
#> [13] reshape2_1.4.2       lazyeval_0.2.0       rmarkdown_1.6.0.9001
#> [16] labeling_0.3         tools_3.3.2          stringr_1.2.0       
#> [19] munsell_0.4.3        yaml_2.1.14          colorspace_1.3-2    
#> [22] htmltools_0.3.6      knitr_1.16           tibble_1.3.3

I noticed yesterday independently that horizontal bxoplots shows as lines - I am not sure yet if it is a bug, or a feature, or it it ca be changed 我昨天独立地注意到水平bxoplots显示为线条 - 我不确定它是否是一个bug或一个功能,或者它可以改变

in your case, I did this 在你的情况下,我这样做了

library(ggplot2)
ggplot(data=mpg) +
  geom_boxplot(aes(y=cty, x=model,fill=model))+
  facet_grid(manufacturer~drv, scales = "free", space = "free")+theme(axis.text.x=element_text(angle=90),legend.position="none")

just reversed x and y, and also the facets=_grid call, added some color and rotated x labels - I think this is what you want just flipped 只是颠倒了x和y,还有facets = _grid调用,添加了一些颜色和旋转的x标签 - 我想这就是你想要翻转的东西

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM