关闭小平面标签的剪切

Luc*_*ler 8 r ggplot2

我有一个带有条带标签的多面图,由于多面的宽度而被剪裁 - 我一直在 Inkscape 中手动删除此剪辑,但想在 R 中执行此操作。请参阅这个可重复的小示例(图形宽度是非-)可协商,需要导出为 .eps 文件):

library(tidyverse)

# Build data frame
df <- data.frame(treatment = factor(c(rep("A small label", 5), rep("A slightly too long label", 5))),
             var1 = c(1, 4, 5, 7, 2, 8, 9, 1, 4, 7),
             var2 = c(2, 8, 11, 13, 4, 10, 11, 2, 6, 10))

# Plot scatter graph with faceting by 'treatment'
p <- ggplot(df, aes(x = var1, y = var2)) + 
  geom_point() + 
  facet_wrap(treatment ~ ., ncol = 2)

# Save graph as .eps
ggsave(filename = "Graph1.eps", plot = p, device = "eps", width = 60, height = 60, units = "mm")
Run Code Online (Sandbox Code Playgroud)

带有剪裁的面标签的散点图

我想要的是这样的,其中面标签延伸到面的宽度之外:

从构面标签中删除片段后的散点图

到目前为止,我已经尝试了以下StackOverflow 问题

# This doesn't affect the strip labels
p2 <- p +
  coord_cartesian(clip = "off")

ggsave(filename = "Graph.eps", plot = p2, device = "eps", width = 60, height = 60, units = "mm")

# This doesn't affect strip labels and results in a blank white graph when exported using ggsave
p3 <- p
p3$layout$clip = "off"

ggsave(filename = "Graph.eps", plot = p3, device = "eps", width = 60, height = 60, units = "mm")

Run Code Online (Sandbox Code Playgroud)

我也尝试过用这种方法从这个问题中关闭layout$clip ,但它有与上面相同的问题,条带标签仍然被剪裁并且 ggsave 导出一个空白文件。

p4 <- ggplot_gtable(ggplot_build(p))
p4$layout$clip[p4$layout$name == "panel"] <- "off"
p4 <- grid.draw(p4)

ggsave(filename = "Graph.eps", plot = p4, device = "eps", width = 60, height = 60, units = "mm")

Run Code Online (Sandbox Code Playgroud)

teu*_*and 9

编辑:从 ggplot2 3.4.0 开始,这已被集成。

ggplot2 github 上有一个带有开放PR 的功能请求,可以使条带剪辑成为可选(免责声明:我提交了问题并打开了 PR)。希望 ggplot2 团队能够批准其用于下一个版本。

同时你可以从 github 下载 PR 并尝试一下。

library(ggplot2) # remotes::install_github("tidyverse/ggplot2#4223")

df <- data.frame(treatment = factor(c(rep("A small label", 5), rep("A slightly too long label", 5))),
                 var1 = c(1, 4, 5, 7, 2, 8, 9, 1, 4, 7),
                 var2 = c(2, 8, 11, 13, 4, 10, 11, 2, 6, 10))

# Plot scatter graph with faceting by 'treatment'
p <- ggplot(df, aes(x = var1, y = var2)) + 
  geom_point() + 
  facet_wrap(treatment ~ ., ncol = 2) +
  theme(strip.clip = "off")

ggsave(filename = "Graph1.eps", plot = p, device = "eps", width = 60, height = 60, units = "mm")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述