英文:
Incomplete png with ggarrange
问题
我有以下问题。我已经用ggarrange合并了两个图形,然后最终用ggsave保存了它们。不幸的是,.png文件看起来有点奇怪。图例旁边的右侧和左侧部分没有填充,但实际上不存在。不幸的是,这导致在LaTeX中的图片看起来不好。我应该如何调整它,以便在图例旁边的下方,空白变成白色。
我的代码:
(merge=ggarrange(gg1, gg2, ncol=2, common.legend = TRUE, legend="bottom"))
ggsave(uni4, filename = "file.png", path = "~/path", width = 24, height = 14, units = "cm", dpi=700)
更新:
df = https://docs.google.com/spreadsheets/d/1oIlJPuIrYcqKOeqTdrXNzZubat-tV0J5q-tvQDqOnGk/edit?usp=sharing
g11 = ggplot() +
geom_smooth(se = F, data = df, aes(x = sc, y = changes, color = factor(nr), group = factor(nr))) +
scale_color_viridis(discrete = TRUE, option = "D")+
scale_fill_viridis(discrete = TRUE) +
theme(legend.position = "bottom")
g21 = ggplot() +
geom_smooth(se=F, data = df, aes(x = sc, y = changes, color = factor(nr), group = factor(nr))) +
scale_color_viridis(discrete = TRUE, option = "D")+
scale_fill_viridis(discrete = TRUE) +
theme(legend.position = "bottom")
(uni11=ggarrange(g11, g21, ncol=2, common.legend = TRUE, legend="bottom"))
ggsave(uni11, filename = "file.png", path = "~/path", width = 24, height = 8, units = "cm", dpi=700)
英文:
I have the following problem. I have merged two plots with ggarrange and then finally saved them with ggsave. Unfortunately the .png files look a bit funny. The right and left part next to the legend is not filled in but does not exist. This leads unfortunately to the fact that the pictures in LaTeX e.g. do not look good. How can I adjust it so that also below next to the legend, the space becomes white.
My code:
(merge=ggarrange(gg1, gg2, ncol=2, common.legend = TRUE, legend="bottom"))
ggsave(uni4, filename = "file.png", path = "~/path", width = 24, height = 14, units = "cm", dpi=700)
UPDATE:
df = https://docs.google.com/spreadsheets/d/1oIlJPuIrYcqKOeqTdrXNzZubat-tV0J5q-tvQDqOnGk/edit?usp=sharing
g11 = ggplot() +
geom_smooth(se = F, data = df, aes(x = sc, y = changes, color = factor(nr), group = factor(nr))) +
scale_color_viridis(discrete = TRUE, option = "D")+
scale_fill_viridis(discrete = TRUE) +
theme(legend.position = "bottom")
g21 = ggplot() +
geom_smooth(se=F, data = df, aes(x = sc, y = changes, color = factor(nr), group = factor(nr))) +
scale_color_viridis(discrete = TRUE, option = "D")+
scale_fill_viridis(discrete = TRUE) +
theme(legend.position = "bottom")
(uni11=ggarrange(g11, g21, ncol=2, common.legend = TRUE, legend="bottom"))
ggsave(uni11, filename = "file.png", path = "~/path", width = 24, height = 8, units = "cm", dpi=700)
答案1
得分: 0
可能修复您的问题的一种方法是使用 ggsave
的 bg
参数手动设置背景填充颜色。
使用基于 mtcars
的最小示例,首先重现您的问题:
library(ggplot2)
library(ggpubr)
gg1 <- gg2 <- ggplot(mtcars, aes(hp, mpg, color = factor(cyl))) +
geom_point()
merge <- ggarrange(gg1, gg2, ncol = 2, common.legend = TRUE, legend = "bottom")
ggsave(merge,
filename = "file.png"
)
现在,要修复左侧和右侧传说中的“灰色”或“黑色”空白问题,我们可以使用 bg="white"
导出:
ggsave(merge,
filename = "file1.png", bg = "white"
)
英文:
One possible fix for your issue would be to manually set the background fill color using the bg
argument of ggsave
.
Using a minimal example based on mtcars
let's first reproduce your issue:
library(ggplot2)
library(ggpubr)
gg1 <- gg2 <- ggplot(mtcars, aes(hp, mpg, color = factor(cyl))) +
geom_point()
merge <- ggarrange(gg1, gg2, ncol = 2, common.legend = TRUE, legend = "bottom")
ggsave(merge,
filename = "file.png"
)
Now, to fix the issue with the "grey" or "black" space on the left and right of the legend we could export with bg="white"
:
ggsave(merge,
filename = "file1.png", bg = "white"
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论