Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

r - ggplot2: How can I set axis breaks separately for each facet in facet_wrap?

I would like to modify the breaks and limits of y-axis of each one of the graphs in the facet_wrap. I want for example reduce the breaks for some of them or I want that they start from 0.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I don't know of a way to modify the axis breaks and ranges of individual facets in a faceted plot. However, another option is to create separate plots for each level of the faceting variable and then lay out all the plots together. Creating each plot individually allows you to have finer control over axis breaks and ranges for each plot.

Here's an example with the built-in mtcars data frame:

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

The code below creates a list of plots, one for each level of cyl. Note the use of scale_y_continuous to set the y-range for each plot. This is just an illustration. You can exert much finer control over the axis ranges and breaks for each plot if you wish.

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

Now, lay out the plots in a single column. We also add a y-axis label.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...