如何在拼凑中设置长多图图形的尺寸?
How to set dimension for long multi-plot figure in patchwork?
我正在学习 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。谢谢。
我认为你 运行 有两个问题:
- 设备的地块大小
- 地块本身的纵横比
第一个将由设备R绘图的大小决定。这些图将尝试填充整个设备。如果您将设备设置为垂直多于水平 space,绘图将自动调整。问题是您正试图在单个设备上包含大量绘图。一张纸,读完会很吃力。
另一个问题告诉 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")
我正在学习 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。谢谢。
我认为你 运行 有两个问题:
- 设备的地块大小
- 地块本身的纵横比
第一个将由设备R绘图的大小决定。这些图将尝试填充整个设备。如果您将设备设置为垂直多于水平 space,绘图将自动调整。问题是您正试图在单个设备上包含大量绘图。一张纸,读完会很吃力。
另一个问题告诉 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")