简体   繁体   English

R / ggplot:带facet_wrap的垂直条带文本

[英]R/ggplot: Vertical strip text with facet_wrap

I'm using ggplot in R to plot several conditions with facet_wrap. 我在R中使用ggplot用facet_wrap绘制几个条件。 I'd like to put the strip with the plot name on the vertical axis on the right instead of on top. 我想把带有情节名称的条带放在右边的垂直轴上,而不是放在顶部。

This is an example: 这是一个例子:

library(ggplot2)
dat<- data.frame(name= rep(LETTERS[1:5], each= 4), value= rnorm(20), time= rep(1:5, 4))
gg<- ggplot(data= dat, aes(x= time, y= value)) +
    geom_point() +
    facet_wrap(~ name, ncol= 1)

在此输入图像描述 Here the plot names (A, B, C, D, E) are on top, I'd like them to be on the right as in here: 这里的情节名称(A,B,C,D,E)位于顶部,我希望它们位于右侧,如下所示:

gg + facet_grid(name ~ .)

在此输入图像描述

Is there an easy switch to do it? 有一个简单的开关来做吗? (I'm not using facet_grid since I'd like to use the options nrow and ncol that come with facet_wrap ). (我没有使用facet_grid ,因为我想使用的选项nrowncol附带facet_wrap )。

Thanks! 谢谢! Dario 达里奥

sessionInfo()
R version 3.0.1 (2013-05-16)
Platform: x86_64-apple-darwin10.8.0 (64-bit)

locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggplot2_0.9.3.1

loaded via a namespace (and not attached):
 [1] colorspace_1.2-4   dichromat_2.0-0    digest_0.6.4       grid_3.0.1        
 [5] gtable_0.1.2       labeling_0.2       MASS_7.3-29        munsell_0.4.2     
 [9] plyr_1.8.1         proto_0.3-10       RColorBrewer_1.0-5 Rcpp_0.11.0       
[13] reshape2_1.2.2     scales_0.2.3       stringr_0.6.2      tools_3.0.1       

For future reference, you can now use strip.position="right" to put them on the right side , eg: 为了将来参考,您现在可以使用strip.position="right" 将它们放在右侧 ,例如:

library(ggplot2)
dat<- data.frame(name= rep(LETTERS[1:5], each= 4), value= rnorm(20), time= rep(1:5, 4))
gg<- ggplot(data= dat, aes(x= time, y= value)) +
    geom_point() +
    facet_wrap(~ name, ncol= 1, strip.position="right")

If you are willing to have the facet labels on the left hand side of the facets then there is a simple solution where the y-axis becomes the x-axis and the x-axis becomes the y-axis. 如果您愿意在小平面的左侧有小平面标签,那么有一个简单的解决方案,其中y轴成为x轴,x轴成为y轴。

library(ggplot2)
library(gridExtra)
library(gridGraphics)

# standard plot, facet labels on top
ggplot(diamonds) + 
  aes(x = carat, y = price) + 
  geom_point() + 
  facet_wrap( ~ cut)

在此输入图像描述

# Use the gridExtra and gridGraphics utilities to rotate the plot.
# This requires some modifications to the axes as well.  Note the use of 
# a negative carat in the aes() call, and text modifications with theme()
grid.newpage()
pushViewport(viewport(angle = 90))
grid.draw(ggplotGrob(

  ggplot(diamonds) + 
    aes(x = price, y = -carat) + 
    geom_point() + 
    facet_wrap( ~ cut) + 
    scale_y_continuous(name = "Carat", breaks = -seq(0, 5, by = 1), labels = seq(0, 5, by = 1)) + 
    theme(axis.text.y = element_text(angle = 270), 
          axis.title.y = element_text(angle = 270), 
          axis.text.x = element_text(angle = 270))
    ))

在此输入图像描述

Moving the facet_wrap labels to the right hand side of the rotated graphic would be done with a rotation angle of -90 . facet_wrap标签移动到旋转图形的右侧将以-90的旋转角度完成。 However, that would have the effective x-axis on top of the plots. 但是,这将在图上方具有有效的x轴。 You'd need to work on code for moving the y-axis labels from the left to right side of the "standard" plot and then rotate as illustrated here by -90 . 你需要处理代码,将y轴标签从“标准”图的左侧移动到右侧,然后按-90旋转,如图所示。

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

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