gpt4 book ai didi

r - 保存具有透明背景的 R 图时出现问题

转载 作者:行者123 更新时间:2023-12-02 03:06:29 33 4
gpt4 key购买 nike

我正在尝试使用 png 格式保存具有透明背景的 r 图。我遵循了 stackoverflow 中的一些推荐方法,但每次我仍然得到白色背景。我的测试日期如下:

structure(list(wd = c(7.5, 22.5, 37.5, 52.5, 67.5, 82.5, 97.5, 
112.5, 127.5, 142.5, 157.5, 172.5, 187.5, 202.5, 217.5, 232.5,
247.5, 262.5, 277.5, 292.5, 307.5, 322.5, 337.5, 352.5), MP1 = c(17.6,
21, 20.5, 26.5, 32.7, 38.3, 40.7, 41.8, 41.6, 44.4, 52.4, 62.5,
70.7, 74.4, 71.1, 66.9, 66.9, 69.4, 69.4, 67.4, 63.4, 55.9, 43.9,
33.9)), .Names = c("wd", "MP1"), class = "data.frame", row.names = c(NA,
-24L))

我尝试了两种方法,但都无法删除背景。

方法一:

library(ggplot2)
library(cairo)

ggplot(dat, aes(wd, MP1)) +
coord_polar( start = 0, direction = 1) +
xlab("")+
ylab("")+
scale_x_continuous(limits = c(0, 360), expand = c(0, 0), breaks = seq(0, 360-1, by = 90), labels=c("North", "East","South", "West")) +
geom_vline(xintercept = seq(0, 360-1, by = 15), colour = "grey90", size = 0.2) +
geom_bar(width=15, stat='identity', fill= "cyan", colour= "white") +
theme_bw() +
theme(panel.border = element_blank(),
legend.key = element_blank(),
axis.ticks = element_blank(),
axis.text.y = element_blank(),
axis.text.x = element_blank(),
panel.grid = element_blank())

Cairo(width = 640, height = 480, file="test.png", type="png",
bg = "transparent")

dev.off()

方法2:

png("test.png", width = 4 * 800,
height = 4 * 800, res = 600)

ggplot(dat, aes(wd, MP1)) +
coord_polar( start = 0, direction = 1) +
xlab("")+
ylab("")+
scale_x_continuous(limits = c(0, 360), expand = c(0, 0), breaks = seq(0, 360-1, by = 90), labels=c("North", "East","South", "West")) +
geom_vline(xintercept = seq(0, 360-1, by = 15), colour = "grey90", size = 0.2) +
geom_bar(width=15, stat='identity', fill= "cyan", colour= "white") +
theme_bw() +
theme(panel.border = element_blank(),
legend.key = element_blank(),
axis.ticks = element_blank(),
axis.text.y = element_blank(),
axis.text.x = element_blank(),
panel.grid = element_blank(),
plot.background = element_rect(fill = NULL,colour = NA))

dev.off()

不幸的是,这两种方法都没有给我提供透明背景,并且如果我添加到 ArcGIS 或 Microsoft Word 文档,仍然显示白色背景。

我真的很感激任何关于我可能做错的事情的建议,因为我没有收到任何错误消息,但只是没有得到透明背景。非常感谢提前

最佳答案

根据 @Molx 和 @aosmith 收到的评论,以下答案对我有用,所以如果有人发现这对他们将来的工作有用,我只是发布:

 ggplot(dat, aes(wd, MP1)) +
coord_polar( start = 0, direction = 1) +
xlab("")+
ylab("")+
scale_x_continuous(limits = c(0, 360), expand = c(0, 0), breaks = seq(0, 360-1, by = 90), labels=c("North", "East","South", "West")) +
geom_vline(xintercept = seq(0, 360-1, by = 15), colour = "grey90", size = 0.2) +
geom_bar(width=15, stat='identity', fill= "cyan", colour= "white") +
theme_bw() +
theme(panel.border = element_blank(),
legend.key = element_blank(),
axis.ticks = element_blank(),
axis.text.y = element_blank(),
axis.text.x = element_blank(),
panel.grid = element_blank(),
panel.grid.minor = element_blank(),
panel.grid.major = element_blank(),
panel.background = element_blank(),
plot.background = element_rect(fill = "transparent",colour = NA))

ggsave("test.png", bg = "transparent")

关于r - 保存具有透明背景的 R 图时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31550435/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com