将 geom_vline 扩展到地块之外

Extend geom_vline outside of plot

我正在尝试将我的 ggplot 图中的 geom_vline 线扩展到图 space 之外并进入轴区域。这样做的目的是让这些线分隔轴标签,以便它可以与旁边的另一个图对齐(见下文)。

一些简短的示例代码(我有更多的行,因此需要水平线来保持直线):

library(ggplot2)
library(cowplot)
library(dplyr)

#play data set
cars.data <- mtcars %>%
      mutate(car_name = rownames(mtcars)) %>%
      slice(1:6)

#I would like vlines to be extend in this plot
p1 <- ggplot(cars.data, aes(x = car_name, y = hp)) +
    geom_point() +
    scale_x_discrete(position = "top") +
    coord_flip() +
    geom_vline(aes(xintercept = seq(1.5, 6.5, 1)), color = "gray60") +
    xlab("")


p2 <- ggplot(cars.data, aes(y = car_name, x = 1)) +
  geom_text(aes(label = disp)) +
  xlab("disp") +
  geom_hline(aes(yintercept = seq(1.5, 6.5, 1)), color = "gray60")+
  theme(axis.title.y = element_blank(),
        axis.title.x = element_text(vjust = 0.5, angle = 30),
        axis.text = element_blank(),
        axis.line = element_blank(),
        axis.ticks = element_blank(),
        panel.background = element_rect(fill = "gray90"))

plot_grid(p1, p2, rel_widths = c(1,0.2))

结果如下图:

我正在寻找的是扩展 p1 的线条,以便它们在地块之间继续,几乎就像地块-table 的混合体。我已经试过了 clip = "off",但似乎没有用。

这是一种入门方法,它不是依赖 ggplot 的轴标签来获得您想要的内容,而是将标签视为数据并构建您自己的标签。有一个问题,我们在这里使用了类似的方法:

一些技巧使这项工作成功:

  • 将轴标签替换为左对齐 geom_text 并缩放以正确放置它
  • 使所有几何图形使用相同的 x 轴变量,以便 cowplot 可以沿该轴对齐
  • 在连接的边上设置负边距,这样面板就会 运行 一个到另一个,看起来像一个连续的图
library(ggplot2)
library(cowplot)
library(dplyr)

theme_set(theme_cowplot()) # I'm using cowplot v1.0.0 which no longer does this by default

p_left <- ggplot(cars.data, aes(x = car_name, y = hp)) +
  geom_vline(aes(xintercept = seq(1.5, 6.5, 1)), color = "gray60") +
  geom_point() +
  scale_x_discrete(breaks = NULL) +
  coord_flip() +
  xlab(NULL) +
  theme(axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.line = element_blank(),
        plot.margin = margin(5, -5, 5, 5, "pt"),
        panel.border = element_blank())

p_middle <- ggplot(cars.data, aes(x = car_name, y = 1)) +
  geom_vline(aes(xintercept = seq(1.5, 6.5, 1)), color = "gray60") +
  geom_text(aes(label = car_name), hjust = 0) +
  scale_x_discrete() +
  scale_y_continuous(expand = expansion(add = c(0.05, 0.5))) +
  coord_flip() +
  labs(x = NULL, y = "") +
  theme(axis.text = element_blank(),
        axis.ticks = element_blank(),
        axis.line = element_blank(),
        panel.grid = element_blank(),
        plot.margin = margin(5, -5, 5, -5, "pt"),
        panel.border = element_blank())

p_right <- ggplot(cars.data, aes(x = car_name, y = 1)) +
  geom_vline(aes(xintercept = seq(1.5, 6.5, 1)), color = "gray60") +
  geom_text(aes(label = disp)) +
  scale_x_discrete() +
  coord_flip() +
  labs(x = NULL, y = "disp") +
  theme(axis.text = element_blank(),
        axis.ticks = element_blank(),
        axis.line = element_blank(),
        panel.grid = element_blank(),
        plot.margin = margin(5, 5, 5, -5, "pt"),
        panel.border = element_blank())


plot_grid(p_left, p_middle, p_right,
  nrow = 1, rel_widths = c(1, 0.4, 0.2),
  align = "h"
)

我只复制了基本的主题更改——您可能需要根据自己的目的进行调整。我还建议深入研究每个单独的地块以逐个调整它们。

您必须自己绘制线条,以确保在您设置 clip = "off" 时它们可以延伸超过绘图边界。我在这里使用 geom_segment() 并在坐标中手动设置限制。

您还需要对齐 plot_grid() 中的两个图,以确保一切正常。

library(ggplot2)
library(cowplot)
library(dplyr)

#play data set
cars.data <- mtcars %>%
  mutate(car_name = rownames(mtcars)) %>%
  slice(1:6)

p1 <- ggplot(cars.data, aes(x = car_name, y = hp)) +
  geom_point() +
  scale_x_discrete(
    name = NULL,
    position = "top"
  ) +
  scale_y_continuous(expand = c(0, 0)) +
  coord_flip(clip = "off", ylim = c(80, 180)) +
  geom_segment(
    data = data.frame(x = seq(1.5, 6.5, 1), ymin = 80, ymax = 240),
    aes(x = x, xend = x, y = ymin, yend = ymax),
    inherit.aes = FALSE,
    color = "gray60"
  ) +
  xlab(NULL) +
  theme_cowplot()


p2 <- ggplot(cars.data, aes(y = car_name, x = 1)) +
  geom_text(aes(label = disp)) +
  xlab("disp") +
  geom_hline(aes(yintercept = seq(1.5, 6.5, 1)), color = "gray60") +
  theme_cowplot() +
  theme(
    axis.title.y = element_blank(),
    axis.title.x = element_text(vjust = 0.5, angle = 30),
    axis.text = element_blank(),
    axis.line = element_blank(),
    axis.ticks = element_blank(),
    panel.background = element_rect(fill = "gray90"),
    plot.margin = margin(7, 7, 7, 0)
  )

plot_grid(p1, p2, rel_widths = c(1,0.2), align = "h", axis = "bt")

reprex package (v0.3.0)

于 2019-12-02 创建