簡體   English   中英

R / ggplot:帶facet_wrap的垂直條帶文本

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

我在R中使用ggplot用facet_wrap繪制幾個條件。 我想把帶有情節名稱的條帶放在右邊的垂直軸上,而不是放在頂部。

這是一個例子:

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)

在此輸入圖像描述 這里的情節名稱(A,B,C,D,E)位於頂部,我希望它們位於右側,如下所示:

gg + facet_grid(name ~ .)

在此輸入圖像描述

有一個簡單的開關來做嗎? (我沒有使用facet_grid ,因為我想使用的選項nrowncol附帶facet_wrap )。

謝謝! 達里奧

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       

為了將來參考,您現在可以使用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")

如果您願意在小平面的左側有小平面標簽,那么有一個簡單的解決方案,其中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))
    ))

在此輸入圖像描述

facet_wrap標簽移動到旋轉圖形的右側將以-90的旋轉角度完成。 但是,這將在圖上方具有有效的x軸。 你需要處理代碼,將y軸標簽從“標准”圖的左側移動到右側,然后按-90旋轉,如圖所示。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM