2016-04-05 33 views
4

我想修改facet_wrap中每個圖形的y軸的斷點和極限。我想例如降低減免他們中的一些或我想,他們開始從0ggplot2:如何分別爲facet_wrap中的每個面設置軸中斷?

ggplot(granuControlLw, aes(distance, group=time)) 
+ geom_line(aes(y = D)) + xlab("Distance from the gate (m)") 
+ geom_point(aes(y = D, shape = factor(time)), size=4.5) + theme_bw() 
+ theme(axis.text.x = element_text(size = 15, angle = 90), axis.text.y = element_text(size = 15, angle = 0)) 
+ scale_x_continuous(breaks=seq(-50,1100,50)) 
+ theme(axis.text = element_text(size = 15),axis.title = element_text(size = 15),legend.title = element_text(size = 15, face = 'bold'),legend.text= element_text(size=15), axis.line = element_line(colour = "gray")) 
+ theme(strip.background = element_blank()) 
+ facet_wrap (evolution~ section, scale="free_y", ncol=1) 

Here is a few raws of my input file: 
evolution time distance section D 
D5  After 680  (> S450) 0.8286370543 
D5  After 710  (> S450) 1.0857412286 
D5  After 950  (> S450) 0.29524528 
D5  After 1010 (> S450) 0.7115190438 
D16 After 680  (> S450) 2.7797109467 
D16 After 710  (> S450) 4.2948672219 
D16 After 950  (> S450) 0.5445345574 
D16 After 1010 (> S450) 2.9139811532 
D25 After 680  (> S450) 5.3764331372 
D25 After 710  (> S450) 6.6094309926 
D25 After 950  (> S450) 0.789626722 
D25 After 1010 (> S450) 6.25184791 
D50 After 680  (> S450) 13.0637943297 
D50 After 710  (> S450) 17.155345894 
D50 After 950  (> S450) 3.2134971025 
D50 After 1010 (> S450) 18.9873626321 
D75 After 680  (> S450) 19.491433335 
D75 After 710  (> S450) 26.1926456265 
D75 After 950  (> S450) 12.4823051787 
D75 After 1010 (> S450) 45.0209667314 

回答

5

我不知道的方式來修改軸斷裂和個人方面的範圍在一個多面情節。然而,另一種選擇是爲每個等級變量創建單獨的繪圖,然後將所有繪圖放在一起。單獨創建每個繪圖允許您更好地控制每個繪圖的軸斷點和範圍。

下面是與內置mtcars數據幀的示例:

library(scales)  # For pretty_breaks 
library(grid)  # For textGrob 
library(gridExtra) # For grid.arrange 
library(cowplot) # For plot_grid 

下面的代碼創建情節的列表,一個用於的cyl每個級別。請注意使用scale_y_continuous來設置每個繪圖的y範圍。這只是一個例證。如果您願意,您可以對每個繪圖的軸範圍和斷點進行更精細的控制。

pl = lapply(sort(unique(mtcars$cyl)), function(i) { 

    p = ggplot(mtcars[mtcars$cyl==i, ], aes(wt, mpg)) + 
    facet_wrap(~cyl) + 
    geom_point() + 
    labs(x="Weight", y="") + 
    scale_y_continuous(limits=c(ifelse(i==4, 10, 0), 1.1 * max(mtcars$mpg[mtcars$cyl==i])), 
         breaks=pretty_breaks(ifelse(i==6, 6, 3))) + 
    scale_x_continuous(limits=range(mtcars$wt)) + 
    theme(plot.margin=unit(c(0, 0.1, 0, -1),"lines")) 

    # Remove x-axis labels and title except for last plot 
    if(i < max(mtcars$cyl)) p = p + theme(axis.text.x=element_blank(), 
             axis.title.x=element_blank()) 

    return(p) 
}) 

現在,將這些圖繪製在一個列中。我們還添加了一個y軸標籤。

grid.arrange(textGrob("MPG", rot=90), plot_grid(plotlist=pl, ncol=1, align="h"), 
      widths=c(0.03,0.97), ncol=2) 

enter image description here

相關問題