0

我正在学习 R,我想使用 R 中的 patchwork 包生成 11 个图(一列)的长而可读的图像。但是,由于图太多,输出图像会失真/压扁并且不可读。这是我的代表:

data(diamonds)
library(ggplot2)
library(patchwork)

a <- ggplot(data = diamonds, aes(y = clarity)) +
  geom_bar(stat = "count", orientation = "y")
b <- ggplot(data = diamonds, aes(y = color)) +
  geom_bar(stat = "count", orientation = "y")
c <- ggplot(data = diamonds, aes(y = table)) +
  geom_bar(stat = "count", orientation = "y")
d <- ggplot(data = diamonds, aes(y = cut)) +
  geom_bar(stat = "count", orientation = "y")
e <- ggplot(data = diamonds, aes(y = x)) +
  geom_bar(stat = "count", orientation = "y")
f <- ggplot(data = diamonds, aes(y = depth)) +
  geom_bar(stat = "count", orientation = "y")
g <- ggplot(data = diamonds, aes(y = carat)) +
  geom_bar(stat = "count", orientation = "y")
h <- ggplot(data = diamonds, aes(y = z)) +
  geom_bar(stat = "count", orientation = "y")

a + b + c + d + e + f + g + h +
  plot_layout(ncol = 1)
         
      

这是生成的图像: 在此处输入图像描述

如果可能的话,我如何锁定方面以便它在 MS Word 中可读甚至可滚动?

如果我可以提供更多信息或需要使用不同的包,请告诉我。最终,我会将此图以 .png 格式导出到 Microsoft Word。谢谢你。

4

1 回答 1

0

我认为您遇到了两个问题:

  1. 设备的地块大小
  2. 地块本身的纵横比

第一个将由设备 R 的大小决定。这些图将尝试填充整个设备。如果您将设备设置为具有比水平空间更大的垂直空间,则绘图将自动调整。问题是您试图在单个设备上包含很多图。对于一张纸,很难阅读所有内容。

另一个问题告诉ggplot您希望如何显示每个图表。在这种情况下,无论设备中包含的区域如何,都将设置 x 和 y 之间的纵横比。

下面,我通过将列diamonds分成两组五个来解决第一个问题。然后我为每个保存一个 PNG 图像,将 PNG 的大小设置为具有 1 英寸边距的信纸。theme(aspect.ratio = 1)我通过设置确保一系列方形图来解决第二个问题(您可以使用它来创建不同的纵横比)。

我使用“不要重复自己”(DRY)编程原则将整个事情包装在一个函数中。

library(ggplot2)
library(patchwork)

bplt <- function(yvar) {
    ggplot(diamonds, aes_(y = as.name(yvar))) + geom_bar() +
        theme(aspect.ratio = 1)
}
wrap_plots(lapply(names(diamonds)[1:5], bplt), ncol = 1)
ggsave("~/tmp/diamond_features1.png", width = 6.5, height = 9, units = "in")
wrap_plots(lapply(names(diamonds)[6:10], bplt), ncol = 1)
ggsave("~/tmp/diamond_features2.png", width = 6.5, height = 9, units = "in")
于 2021-08-25T16:01:01.407 回答