Transparency in gtable Objects
Asked Answered
S

1

8

I am trying to produce png file of ggplot objects with transparent backgrounds. When I was using the ggplot objects directly with panel.background=element_rect(fill = "transparent",colour = NA) I was getting the results I wanted.

I am now trying to make some modifications to the plot (adding labels to the facets like this How do you add a general label to facets in ggplot2?)

Instead of getting a transparent background, when I plot the modified gtable object, the background is light grey with thin white lines.

enter image description here

Whereas if I plot the ggplot object directly it does support the transparency

enter image description here

Here is the code I am using:

data <- data.table(a = seq(1,5), b=sample.int(5,50,replace=TRUE), c=rnorm(100))

plotSlices <- function(input, rowfacet, colfacet, metric, label, facetlabels=FALSE){
  calc <- substitute(metric)
  bygroup<-c(rowfacet,colfacet)
  aggregates <- input[,eval(calc),by=bygroup]

  chart <- ggplot(aggregates) + 
    geom_bar(stat="identity") + 
    aes(x="", y=V1, fill=V1>0) + 
    facet_grid(as.formula(sprintf("%s ~ %s", rowfacet, colfacet))) +
    coord_flip() +
    xlab("") +
    ylab(label) +
    theme(legend.position = "none", 
          axis.title=element_text(size=16, color="white"),
          axis.ticks.y=element_blank(),
          panel.grid.major.y=element_blank(),
          panel.grid.major.y=element_line(colour = "grey25"),
          panel.grid.minor.x=element_blank(),
          plot.background=element_rect(fill = "transparent",colour = NA),
          panel.background=element_rect(fill = "transparent",colour = NA)
    )

  if (facetlabels) {
    return(LabelFacets(chart, rowfacet, colfacet))  
  }
  else {
    return(chart)
  }
}
LabelFacets <- function(plot, rowfacet, colfacet) {
  grob <- ggplotGrob(plot)
  grob <- gtable_add_cols(grob, grob$widths[[7]])
  grob <- gtable_add_grob(grob, list(rectGrob(gp = gpar(col = NA, fill = gray(0.5))),
                                     textGrob(rowfacet, rot = -90, gp = gpar(col = gray(1)))),
                          4, 14, 12, name = paste(runif(2)))
  grob <- gtable_add_rows(grob, grob$widths[[2]], 2)
  grob <- gtable_add_grob(grob, list(rectGrob(gp = gpar(col = NA, fill = gray(0.5))),
                                     textGrob(colfacet, gp = gpar(col = gray(1)))),
                          3, 4, 3, 12, name = paste(runif(2)))
  return(grob)
}
png(filename = "test.png", bg = "transparent")
plot(plotSlices(data, "a", "b", mean(c), "Label", FALSE))
dev.off()

png(filename = "test2.png", bg = "transparent")
plot(plotSlices(data, "a", "b", mean(c), "Label", TRUE))
dev.off()
Signboard answered 26/9, 2013 at 17:56 Comment(2)
Does it make a difference if you modified grob <- gtable_add_cols(grob, grob$widths[[7]])Interconnect
don't use plot() for a gtable, use grid.draw() instead.Sovereignty
S
10

Baptiste solved it for me. The problem was using plot() to show a gtable object. Instead I should have been using grid.draw().

Signboard answered 26/9, 2013 at 22:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.