【发布时间】:2020-05-13 18:59:21
【问题描述】:
我在使用来自egg 库的ggarrange() 的绘图时遇到问题。当我可视化每个图(使用 RStudio)时,它们是完美的。但是,当我将三个图合并到一个图中时,每个网格的相对大小相对于每个图的大小都会发生变化,并且注释文本会相互重叠,并且字体的相对大小也会发生变化。这似乎是一个可视化问题而不是代码问题,但我不知道。
library(ggplot2)
library(egg)
data <-
structure(list(Treatment = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L),
.Label = c("A","B", "C", "D"),
class = "factor"),
Var1 = c(10L, 12L, 13L, 9L, 14L, 1L, 2L, 1L, 4L, 5L, 5L, 4L, 3L, 8L, 4L, 20L, 20L, 26L, 24L, 17L),
Var2 = c(2.5, 2.6, 2.3, 2.8, 2.9, 1.5, 1.3, 1.5, 1.4, 1.3, 1.6, 1.7, 1.6, 1.8, 1.6, 3.2, 3.5, 3.3, 3.2, 3.1),
Var3 = c(1.1,1.2, 1.1, 1.1, 1.2, 1.4, 1.5, 1.4, 1.5, 1.4, 1.3, 1.3, 1.2, 1.3, 1.3, 0.9, 0.85, 0.9, 0.8, 0.85)),
class = "data.frame",
row.names = c(NA, -20L))
figura2a <-
ggplot(data, aes(x = Var2, y = Var1)) +
geom_point(aes(
color = Treatment,
shape = Treatment,
size = 2.5
)) +
theme(legend.position = "bottom") +
geom_smooth(method = 'lm',
colour = "black",
se = FALSE) +
scale_colour_manual(values = c(
"lightsteelblue4",
"salmon",
"purple3",
"royalblue2",
"seagreen3"
)) +
scale_shape_manual(values = c(15, 16, 25, 18, 17)) +
theme_light() +
theme(
axis.text.x = element_text(colour = "black", size = 22),
axis.text.y = element_text(colour = "black", size = 22)
) +
theme(text = element_text(
size = 22,
family = "Arial",
color = "black"
)) +
theme(legend.title = element_text(size = 20, color = "black"),
legend.text = element_text(size = 20)) +
scale_size(guide = 'none') +
guides(shape = guide_legend(override.aes = list(size = 4))) +
xlab("Var2") +
ylab("Var1") +
theme(legend.position = "none") +
annotate(
geom = "text",
x = 3,
y = 4,
label = "R^2 == 0.8639",
parse = TRUE,
color = "black",
size = 5
) +
annotate(
geom = "text",
x = 3,
y = 5.5,
label = "p < 0.001",
color = "black",
size = 5.5,
fontface = 'italic',
check_overlap = TRUE
)
figura2a
figura2b <-
ggplot(data, aes(x = Var3, y = Var1)) +
geom_point(aes(
color = Treatment,
shape = Treatment,
size = 2.5
)) +
theme(legend.position = "bottom") +
geom_smooth(method = 'lm',
colour = "black",
se = FALSE) +
scale_colour_manual(values = c(
"lightsteelblue4",
"salmon",
"purple3",
"royalblue2",
"seagreen3"
)) +
scale_shape_manual(values = c(15, 16, 25, 18, 17)) +
theme_light() +
theme(
axis.text.x = element_text(colour = "black", size = 22),
axis.text.y = element_text(colour = "black", size = 22)
) +
theme(text = element_text(
size = 22,
family = "Arial",
color = "black"
)) +
theme(legend.title = element_text(size = 20, color = "black"),
legend.text = element_text(size = 20)) +
scale_size(guide = 'none') +
guides(shape = guide_legend(override.aes = list(size = 4))) +
xlab("Var3") +
ylab("Var1") +
theme(legend.position = "none") +
annotate(
geom = "text",
x = 1.2,
y = 22,
label = "R^2 == 0.8252",
parse = TRUE,
color = "black",
size = 5
) +
annotate(
geom = "text",
x = 1.2,
y = 20,
label = "p < 0.001",
color = "black",
size = 5.5,
fontface = 'italic'
)
figura2b
figura2 <- ggarrange(figura2a, figura2b, ncol = 1, nrow = 2)
由reprex package (v0.3.0) 于 2020-05-16 创建
【问题讨论】:
-
请使用
dput(data)或dput(head(data))并粘贴到此处。此外,编辑您的问题并使用正确的格式将数据和代码放在那里。您还可以阅读有关reprex库(tidyverse的一部分)的信息 - 生成可重复的示例非常方便。 -
我试图改进我的问题。提前致谢!
-
我同意设置注释的相对位置很痛苦。如果您只需要最终的情节(
figura2),那么您可以在ggsave()和x和y中使用height和width比率在annotate()中获得正确的情节之间的比率所以注释放置正确。我会尝试类似figura2 <- (figura2a, figura2b, ncol = 1, nrow = 2) + ggsave('figura2.png', width = 12, height = 8)。 -
非常感谢!我发现如何找到合适的宽度和高度有点直观,但它确实有效!