How to control ylim for a faceted plot with different scales in ggplot2?

后端 未结 3 1450
执笔经年
执笔经年 2021-01-01 11:41

In the following example, how do I set separate ylims for each of my facets?

qplot(x, value,  data=df, geom=c(\"smooth\")) + facet_grid(variable ~ ., scale=\         


        
3条回答
  •  粉色の甜心
    2021-01-01 12:08

    There are actually two packages that solve that problem now: https://github.com/zeehio/facetscales, and https://github.com/teunbrand/ggh4x. I would recommend using ggh4x because it has very useful tools, such as facet grid multiple layers (having 2 variables defining the rows or columns), scaling the x and y-axis as you wish in each facet, and also having multiple fill and colour scales. For your problems the solution would be like this:

    library(ggh4x)
    
    scales <- list(
      # Here you have to specify all the scales, one for each facet row in your case
      scale_y_continuous(limits = c(2,10),
      scale_y_continuous(breaks = c(3, 4))
    )
    qplot(x, value,  data=df, geom=c("smooth")) +
     facet_grid(variable ~ ., scale="free_y") +
     facetted_pos_scales(y = scales)
    

提交回复
热议问题