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

查看:27
本文介绍了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_continuouscoord_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.

推荐答案

preliminaries

为每个面的 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 函数:找出哪个组对应于给定的一组限制......

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天全站免登陆