如何用不连续的y轴绘制刻面 [英] How to plot facets with discontinuous y-axis

查看:60
本文介绍了如何用不连续的y轴绘制刻面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绘制具有不连续y轴的图,但是无法使构面标题仅显示一次:

I am trying to produce a plot with a discontinuous y-axis but can't get the facet titles to only show once:

示例数据:

data(mpg)
library(ggplot2)
> ggplot(mpg, aes(displ, cty)) +
+     geom_point() +
+     facet_grid(. ~ drv)

经过大量挖掘,看来在 ggplot2 中这是不可能的,但是我发现了 gg.gap 包.但是,此程序包将复制情节每个部分的构面标题.假设我要在y轴上从22-32断开,如下所示:

After much digging it appears that this is impossible in ggplot2, but I have discovered the gg.gap package. However, this package replicates the facet titles for each segment of the plot. Let's say I want a break in the y axis from 22-32 as follows:

library(gg.gap)
gg.gap(plot = p,
       segments = c(22, 32),
       ylim = c(0, 35))

每个地块都出现了Facet标题,但这显然在外观上令人迷惑和可怕.对于任何人都可以提供的帮助的任何见解,我将不胜感激!我很困惑.

Facet titles appear for each plot segment but this is clearly pretty confusing and terrible aesthetically. I would be grateful for any insight of help anyone could provide! I'm stumped.

我知道如果我在基准R中作图,这是可能的,但由于其他限制,我无法做到(我需要 ggplot2 提供的图形/语法.

I know this is possible if I plot in base R, but given other constraints I am unable to do so (I need the graphics/grammar provided by ggplot2.

提前谢谢!

推荐答案

这是一个丑陋的解决方法.想法是将折断部分中的y值设置为 NA ,以便在那里不绘制任何点.然后,我们在 findInterval()上加上轴的中断点(为负,因为我们要保留从下到上的轴).最后,我们使用 ggh4x :: force_panelsizes()手动调整面板的大小,以将第二个面板的高度设置为0.完全免责声明,我写了ggh4x,所以我有偏见.

This is a bit of an ugly workaround. The idea is to set y-values in the broken portion to NA so that no points are drawn there. Then, we facet on a findInterval() with the breaks of the axes (negative because we want to preserve bottom-to-top axes). Finally we manually resize the panels with ggh4x::force_panelsizes() to set the 2nd panel to have 0 height. Full disclaimer, I wrote ggh4x so I'm biased.

一些细节:通过将相关主题元素设置为空白,可以隐藏沿y方向的条带.另外,理想情况下,您应该计算出上切面相对于下切面的比例,并用该数字替换 0.2 .

A few details: the strips along the y-direction are hidden by setting the relevant theme elements to blank. Also, ideally you'd calculate what proportion the upper facet should be relative to the lower facet and replace the 0.2 by that number.

library(ggplot2)
library(ggh4x)

ggplot(mpg, aes(displ, cty)) +
  geom_point(aes(y = ifelse(cty >= 22 & cty < 32, NA, cty))) +
  facet_grid(-findInterval(cty, c(-Inf, 22, 32, Inf)) ~ drv, 
             scales = "free_y", space = "free_y") +
  theme(strip.background.y = element_blank(),
        strip.text.y = element_blank(),
        panel.spacing.y = unit(5.5/2, "pt")) +
  force_panelsizes(rows = c(0.2, 0, 1))
#> Warning: Removed 20 rows containing missing values (geom_point).

箱线图的替代方法:

您可以复制数据并操纵位置比例以显示所需的内容,而不是检查休息时间的位.我们依靠坐标系对数据的裁剪来裁剪图形对象.

Instead of censoring the bit on the break, you can duplicate the data and manipulate the position scales to show what you want. We rely on the clipping of the data by the coordinate system to crop the graphical objects.

library(ggplot2)
library(ggh4x)

ggplot(mpg, aes(class, cty)) +
  geom_boxplot(data = ~ transform(., facet = 2)) +
  geom_boxplot(data = ~ transform(., facet = 1)) +
  facet_grid(facet ~ drv, scales = "free_y", space = "free_y") +
  facetted_pos_scales(y = list(
    scale_y_continuous(limits = c(32, NA), oob = scales::oob_keep, # <- keeps data
                       expand = c(0, 0, 0.05, 0)),
    scale_y_continuous(limits=  c(NA, 21), oob = scales::oob_keep,
                       expand = c(0.05, 0, 0, 0))
  )) +
  theme(strip.background.y = element_blank(),
        strip.text.y = element_blank())

这篇关于如何用不连续的y轴绘制刻面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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