ggplot2更改每个单独的面板的轴限制 [英] ggplot2 change axis limits for each individual facet panel

查看:128
本文介绍了ggplot2更改每个单独的面板的轴限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

library(tidyverse)
ggplot(mpg, aes(displ, cty)) + 
  geom_point() + 
  facet_grid(rows = vars(drv), scales = "free")

上面的ggplot代码由三个面板4fr组成.我希望每个面板的y轴限制如下:

The ggplot code above consists of three panels 4, f, and r. I'd like the y-axis limits to be the following for each panel:

Panel y-min y-max breaks
----- ----- ----- ------
4     5     25    5
f     0     40    10
r     10    20    2

如何修改我的代码以完成此操作?不确定scale_y_continuous是否更有意义,或者coord_cartesian,还是两者的某种组合.

How do I modify my code to accomplish this? Not sure if scale_y_continuous makes more sense or coord_cartesian, or some combination of the two.

推荐答案

初步

为每个构面的y轴定义原始图和所需参数:

preliminaries

Define original plot and desired parameters for the y-axes of each facet:

library(ggplot2)
g0 <- ggplot(mpg, aes(displ, cty)) + 
    geom_point() + 
    facet_grid(rows = vars(drv), scales = "free")

facet_bounds <- read.table(header=TRUE,
text=                           
"drv ymin ymax breaks
4     5     25    5
f     0     40    10
r     10    20    2",
stringsAsFactors=FALSE)

版本1:放入虚假数据点

这不符合breaks规范,但是可以正确地确定界限:

version 1: put in fake data points

This doesn't respect the breaks specification, but it gets the bounds right:

定义一个新的数据框,其中包含每个drv的最小值/最大值:

Define a new data frame that includes the min/max values for each drv:

ff <- with(facet_bounds,
           data.frame(cty=c(ymin,ymax),
                      drv=c(drv,drv)))

将它们添加到绘图中(由于xNA,因此不会进行绘图,但是它们仍用于定义比例尺)

Add these to the plots (they won't be plotted since x is NA, but they're still used in defining the scales)

g0 + geom_point(data=ff,x=NA)

这与expand_limits()相似,不同之处在于该功能适用​​于所有面板或所有图".

This is similar to what expand_limits() does, except that that function applies "for all panels or all plots".

这很丑陋,取决于每个组的范围是否唯一.

This is ugly and depends on each group having a unique range.

library(dplyr)
## compute limits for each group
lims <- (mpg
    %>% group_by(drv)
    %>% summarise(ymin=min(cty),ymax=max(cty))
)

突破功能:弄清楚哪一组对应于它所赋予的极限值...

Breaks function: figures out which group corresponds to the set of limits it's been given ...

bfun <- function(limits) {
    grp <- which(lims$ymin==limits[1] & lims$ymax==limits[2])
    bb <- facet_bounds[grp,]
    pp <- pretty(c(bb$ymin,bb$ymax),n=bb$breaks)
    return(pp)
}
g0 + scale_y_continuous(breaks=bfun, expand=expand_scale(0,0))

这里的另一个丑陋之处在于,我们必须设置expand_scale(0,0)以使限制与组限制完全相等,这可能不是您想要绘图的方式...

The other ugliness here is that we have to set expand_scale(0,0) to make the limits exactly equal to the group limits, which might not be the way you want the plot ...

如果breaks()函数也可以通过某种方式传递有关当前正在计算哪个面板的信息,那将是很好的...

It would be nice if the breaks() function could somehow also be passed some information about which panel is currently being computed ...

这篇关于ggplot2更改每个单独的面板的轴限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆