Home > Net >  pdf image is exporting with a border using ggplot2 in R?
pdf image is exporting with a border using ggplot2 in R?

Time:10-02

So im having this issue that when I export a pdf of a certain type of plot, the plot is being drawn with a border around it. For example, if I create a plot like so:

library(reshape)
library(ggplot2)
library(ggnewscale)

# Create matrix
set.seed(1701)
a <- sample(1:10,100, replace=TRUE)
s <- matrix(a, nrow = 5, ncol=5)
s[upper.tri(s)] = t(s)[upper.tri(s)]
rownames(s) <- colnames(s) <- paste0("x", 1:5)
diag(s) <- 0
sDf <- melt(s)

# create diagonal values
diagDf <- data.frame(
  var1 = c(paste0("x", 1:5)),
  var2 = c(paste0("x", 1:5)),
  val = c(2,5,3,1,5)
)
colPal <- rev(colorspace::sequential_hcl(palette = "Blues 3", n = 100))
colPal2 <- rev(colorspace::sequential_hcl(palette = "Reds 3", n = 100)) 

# make plot
ggplot(sDf, aes(X1,X2))   
  geom_tile(aes(fill = value))  
  scale_fill_gradientn(colors = colPal)  
  new_scale_fill()  
  geom_tile(data = diagDf, aes(var1, var2, fill = val),  color = "black",
            size = 0)  
  scale_fill_gradientn(colors = colPal2)  
  theme(aspect.ratio = 1) 

This will produce a plot in my plot window that looks like this: example with no border

However, if I export a pdf of the image, it draws a black border around the diagonal, like this: example with border

Now, I realise that it must be something to do with the lines:

 geom_tile(data = diagDf, aes(var1, var2, fill = val),  color = "black",
            size = 0)

Specifically, color = "black", size = 0. I would like to keep this line in the code to allow a user to turn on and off the border by way of a function argument which controls the size of the border. So, for example, in a larger function I would have something like

if(border){
    lineSize <- 0.2
  }else{
    lineSize <- 0
  }

and set size = lineSize. The idea being, when lineSize = 0 then size will equal zero, and then the line will be drawn with zero size (which should effectively be the same as not drawing the line at all).

And this works in the plot window of RStudio.... but when I export a pdf with size = 0, the black border is drawn on the pdf. This doesn't happen if I export a PNG.

Any ideas what is happening here? Im using a MacBook Pro (if that matters?!)

CodePudding user response:

First create the plot without the borders:

p <- ggplot(sDf, aes(Var1,Var2))   
  geom_tile(aes(fill = value))  
  scale_fill_gradientn(colors = colPal)  
  new_scale_fill()  
  geom_tile(data = diagDf, aes(var1, var2, fill = val))  
  scale_fill_gradientn(colors = colPal2)  
  theme(aspect.ratio = 1) 

enter image description here

Then you can modify the border color/size if the user asks for it:

if (border) {
  p$layers[[2]]$aes_params$colour = 'black'
  p$layers[[2]]$aes_params$size = 0.2
}

enter image description here

CodePudding user response:

Have you tried playing with the margins at the end of your ggplot?

theme(plot.margin = unit(c(2,2,2,2), "cm"))

With the following rule for each margin: unit(Top,Right,Bottom,Left)

Example:

pdf(file = "example2.pdf")

ggplot(mpg, aes(displ, hwy, colour = class))   
geom_point()  
theme(plot.margin = unit(c(2,2,2,2), "cm"))

dev.off()
  • Related