R: How do I use coord_cartesian on facet_grid with free-ranging axis

前端 未结 3 1283
礼貌的吻别
礼貌的吻别 2020-12-01 18:26

Consider some facet_grid plot

mt <- ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) + geom_point() 
mt + facet_grid(vs ~ am, scales = \"fr         


        
3条回答
  •  南笙
    南笙 (楼主)
    2020-12-01 19:28

    So, this is an old question, but I wanted to share a way that I found. The simplest seems to be to add artificial points to your data, e.g. at the lower left and top right of your data for each plot. Put this information into a separate data frame, then add it to the plot with geom_point using the parameter alpha=0 to make the points invisible. Set the scaling of the axis in facet_wrap to "free_x" or "free_y", depending on what you need.

    Now ggplot scales every facet separately to accommodate the invisible points that you added. A bit hacky, but works nicely.

提交回复
热议问题