Ggplot2: boxplot with facet_grid and free scale

I am trying to have free scales on a Boxplot faceted image.

Using this sample dataset, if I try this:

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

Wrong rectangular graph http://dl.dropbox.com/u/9788680/plot1.png

Here, free scales are implemented exactly as we would like, with different scales for the y axis, depending on the number of available factors for the horizontal facet rule. Boxes, however, are not correctly shown (i.e., as solid lines instead of boxes). When I was looking for a solution, I found that I had to use the coord_flip () function to display boxplot correctly, i.e.

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

Plan the rectangle, but without scaling http://dl.dropbox.com/u/9788680/plot2.png

In the above figure, the boxes are now correct. However, the free scale for the coefficients (for example, along the y axis) is deleted. Now, for each horizontal line of facets, all available factors for the dataset are now available, not just the factors available for each face (as shown in Figure 1).

I would like to know how I can get the correct face with a free scale on both axes by correctly representing the box.

If someone can point me in the right direction, I would appreciate it.

Thanks.

+7
source share
2 answers

The desired behavior is supported at least by 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 
+1
source

I noticed yesterday, independently, that horizontal bxoplots are displayed as strings - I'm still not sure if this is an error or function, or it will be changed

in your case, I did it

 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 changed x and y, as well as calling facets = _grid, added some color and rotated x labels - I think this is what you want to just flip

0
source

All Articles