ggplot2:boxplot with facet_grid和自由缩放 [英] ggplot2: boxplot with facet_grid and free scale

查看:103
本文介绍了ggplot2:boxplot with facet_grid和自由缩放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Boxplot图像上自由缩放刻面。



使用这个示例数据集,如果我试试这个:

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

剧情不正确的boxplot http://dl.dropbox.com/u/9788680/plot1.png



这里,免费的比例尺按照我的意愿实现,y轴的不同尺度取决于水平面规则的可用因子数量。然而,箱形图没有被正确描绘(即以实线代替箱形图)。
当搜索一个解决方案时,我发现我应该使用coord_flip()来使boxplot被正确描述,即

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

绘制正确的boxplot,但不缩放http://dl.dropbox.com/u/9788680/plot2.png



在上图中,箱形图现在是正确的。但是,这些因素的自由标度(如y轴)将被删除。现在,对于每个水平刻面线,现在包含整个数据集中的所有可用因子,而不是仅包含每个刻面可用的因子(如图1所示)。

我想知道如何在两个轴上都有一个自由缩放的正确刻面,正确描绘了盒形图。

如果有人能指引我正确的方向,我会很感激。



谢谢。

解决方案

ggplot2 2.2.1。


 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()


$ b

sessionInfo()
#> R版本3.3.2(2016-10-31)
#>平台:x86_64-apple-darwin13.4.0(64位)
#>运行于:OS X El Capitan 10.11.6
#>
#>区域设置:
#> [1] en_US.UTF-8 / en_US.UTF-8 / en_US.UTF-8 / C / en_US.UTF-8 / en_US.UTF-8
#>
#>附加的基本包:
#> [1] stats graphics grDevices utils datasets methods base
#>
#>其他附加软件包:
#> [1] ggplot2_2.2.1
#>
#>通过命名空间加载(并且未附加):
#> [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] labels_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 am trying to have free scales on a Boxplot image with faceting.

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

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. The boxplots are however not correctly depicted (i.e. 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, i.e.

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

In above picture, the boxplots are now correct. However, the free scale for the factors (so on the y-axis) are removed. 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).

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.


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

这篇关于ggplot2:boxplot with facet_grid和自由缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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