How to make graphics with transparent background in R using ggplot2?
Create the initial plot:
library(ggplot2)
d <- rnorm(100)
df <- data.frame(
x = 1,
y = d,
group = rep(c("gr1", "gr2"), 50)
)
p <- ggplot(df) + stat_boxplot(
aes(
x = x,
y = y,
color = group
),
fill = "transparent" # for the inside of the boxplot
)
The fastest way to modify the plot above to have a completely transparent background is to set theme()
's rect
argument, as all the rectangle elements inherit from rect
:
p <- p + theme(rect = element_rect(fill = "transparent"))
p
A more controlled way is to set theme()
's more specific arguments individually:
p <- p + theme(
panel.background = element_rect(fill = "transparent",
colour = NA_character_), # necessary to avoid drawing panel outline
panel.grid.major = element_blank(), # get rid of major grid
panel.grid.minor = element_blank(), # get rid of minor grid
plot.background = element_rect(fill = "transparent",
colour = NA_character_), # necessary to avoid drawing plot outline
legend.background = element_rect(fill = "transparent"),
legend.box.background = element_rect(fill = "transparent"),
legend.key = element_rect(fill = "transparent")
)
p
ggsave()
offers a dedicated argument bg
to set the
Background colour. If
NULL
, uses theplot.background
fill value from the plot theme.
To write a ggplot object p
to filename
on disk using a transparent background:
ggsave(
plot = p,
filename = "tr_tst2.png",
bg = "transparent"
)
Transparent background graph with ggplot2 in high resolution, R
You need to specify the dimension and resolution in png
. This works for me
png("graph.png", width=20, height=15, units="cm", res=800, bg="transparent")
gridExtra::grid.arrange(o1, o2, ncol=1)
dev.off()
You can also use ggsave
. Here I use cowplot::plot_grid
to combind o1
& o2
o3 <- cowplot::plot_grid(o1, o2, nrow = 2)
ggsave(plot = o3, file = "graph2.png",
type = "cairo-png", bg = "transparent",
width = 20, height = 15, units = "cm", dpi = 800)
R/ggplot2: Add png with transparency info
I think that the alpha channel is being respected here. It's just that inset_element
draws a white canvas element first. What you are trying to do arguably isn't an inset, more of an annotation.
I think using annotation_custom
from ggplot2
is more appropriate here (and means one less package to load).
library(ggplot2)
library(png)
img1 <- grid::rasterGrob(readPNG("bg.png"))
img2 <- grid::rasterGrob(readPNG("image1.png"))
ggp <- ggplot(data.frame()) +
geom_point() +
theme_void() +
annotation_custom(img1) +
annotation_custom(img2, xmax = 0.5, ymax = 0.5)
ggp
Panel transparency in ggplot
Well, the trick is obvious. I have misunderstood what panel and plot background is. So this one should work:
# third option
p <- p + theme(
panel.grid.minor = element_blank(),
panel.grid.major = element_blank(),
plot.background = element_rect(fill = "transparent", colour = NA)
)
Add png to {ggplot2} that has transparent background and was rotated via {magick}
This doesn't really have anything to do with ggplot
specifically. The rotate changes the size of the image but does not set the background transparent. The image will always have a rectangular shape. You need to set the background so magick knows what to fill in the newly created space with
img_rot <- magick::image_read(url) %>%
magick::image_background("none") %>%
magick::image_rotate(30) %>%
grid::rasterGrob()
ggplot's transparent background changes after ShinyApp resizing
It seems as though when you run a plot in shiny with renderPlot it saves the plot as a variable so that when you resize the page the plot is not rerendered, it just shows the image again. This seems to be having issues with the transparent background (maybe due to a background being set when it is saved as a variable? I'm not sure on this point). To prevent this, set the execOnResize option to TRUE in renderPlot, this will redraw the plot instead of resize the saved image. For example:
output$boxplot <- renderPlot({
ggplot(df, aes(x=id, y=a, group=id)) +
geom_boxplot(aes(fill=id)) +
facet_grid(~id, margins = T) +
theme(rect=element_blank(),
panel.grid = element_blank(),
panel.background= element_blank(),
plot.background = element_blank()
)
}, bg="transparent", execOnResize = TRUE)
ggplot2 png file without background
set the bg argument to "white", e.g.
ggsave("test.png", dpi = 300, bg = "white")
This argument will be passed to grDevices::png
via the ...
argument. bg
controls the background of the device.
Related Topics
The Condition Has Length > 1 and Only the First Element Will Be Used in If Else Statement
Referring to Data.Table Columns by Names Saved in Variables
Add a Row by Reference at the End of a Data.Table Object
Print Unicode Character String in R
Select Rows of a Matrix That Meet a Condition
Similarity Scores Based on String Comparison in R (Edit Distance)
Getting the Last N Elements of a Vector. Is There a Better Way Than Using the Length() Function
Changing the Line Type in the Ggplot Legend
How to Reorder a Legend in Ggplot2
What Is the Most Useful R Trick
How to Directly Select the Same Column from All Nested Lists Within a List
R Shiny Rest API Communication
Shiny Slider on Logarithmic Scale
Calculating Mean for Every N Values from a Vector
What Does "S3 Methods" Mean in R