issue saving R plot with transparent background -


i trying save r plot transparent background png format. have followed few recommended method in stackoverflow every time still getting white background. test date follows:

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)) 

i tried 2 methods both fail remove background.

method 1:

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, = 90), labels=c("north", "east","south", "west")) +   geom_vline(xintercept = seq(0, 360-1, = 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() 

method 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, = 90), labels=c("north", "east","south", "west")) +   geom_vline(xintercept = seq(0, 360-1, = 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() 

unfortunately neither method gives me transparent background , still show white background if add arcgis or microsoft word document.

i appreciate advise possibly doing wrong not getting error messages not getting transparent background. many in advance

based on comment received @molx , @aosmith following answer worked me posting if find useful in future work:

 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, = 90), labels=c("north", "east","south", "west")) +       geom_vline(xintercept = seq(0, 360-1, = 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") 

Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -