Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove border in ggplot2 [duplicate]

I would like to remove the white border in a .png output file, using ggplot2. I'm using Windows 10 with Rstudio, ggplot2, and geom_raster. After spending time searching on forums, and playing with some parameters, I ended up with this codes (that still does not work):

library(ggplot2)
library(datasets)

png(file = "Out.png")

par(mar=rep(0, 4), plt=c(0.1,0.9,0.1,0.9), xpd=NA)

ggplot(faithfuld, aes(waiting, eruptions)) +
geom_raster(aes(fill = density))+
theme(axis.line        = element_blank(),
      axis.text        = element_blank(),
      axis.ticks       = element_blank(),
      axis.title       = element_blank(),
      panel.background = element_blank(),
      panel.border     = element_blank(),
      panel.margin = unit(0,"null"),
      legend.position = "none",
      panel.grid.major = element_blank(),
      panel.grid.minor = element_blank(),
      plot.background  = element_blank(),
      plot.margin = rep(unit(0,"null"),4))

dev.off()

This code gives this png:

enter image description here

like image 315
Maurice clere Avatar asked May 08 '26 07:05

Maurice clere


1 Answers

Even when it is a little bit hacky you can use the cowplot-Package to achieve that as follows:

library(ggplot2)
library(datasets)
require(cowplot)
base <- ggplot(faithfuld, aes(waiting, eruptions)) +
  geom_raster(aes(fill = density)) +
  theme_nothing() + labs(x = NULL, y = NULL) 
plot_grid(base, scale=1.1)

- theme_nothing() + labs(x = NULL, y = NULL) is a short handle for what you did with theme(...)
- plot_grid(..., scale=1.1) is the important part as it resizes the plot to overlap the white border.

This gives you:

enter image description here

like image 174
Rentrop Avatar answered May 09 '26 22:05

Rentrop



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!