“Incomplete png with ggarrange” 只返回翻译好的部分。

huangapple go评论59阅读模式
英文:

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)

“Incomplete png with ggarrange” 只返回翻译好的部分。

英文:

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)

“Incomplete png with ggarrange” 只返回翻译好的部分。

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

可能修复您的问题的一种方法是使用 ggsavebg 参数手动设置背景填充颜色。

使用基于 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"
)

“Incomplete png with ggarrange” 只返回翻译好的部分。

英文:

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 &lt;- gg2 &lt;- ggplot(mtcars, aes(hp, mpg, color = factor(cyl))) +
  geom_point()

merge &lt;- ggarrange(gg1, gg2, ncol = 2, common.legend = TRUE, legend = &quot;bottom&quot;)

ggsave(merge,
  filename = &quot;file.png&quot;
)

“Incomplete png with ggarrange” 只返回翻译好的部分。

Now, to fix the issue with the "grey" or "black" space on the left and right of the legend we could export with bg=&quot;white&quot;:

ggsave(merge,
  filename = &quot;file1.png&quot;, bg = &quot;white&quot;
)

“Incomplete png with ggarrange” 只返回翻译好的部分。

huangapple
  • 本文由 发表于 2023年7月17日 22:17:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76705376.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定