R Shiny web, transparent backgrounds of plots when using ggplots

2,410 views
Skip to first unread message

Timothy Tuti

unread,
Jun 17, 2013, 12:34:49 PM6/17/13
to shiny-...@googlegroups.com

I have a plot that renders with a white background even though I have this in my code:

output$malPie <- renderPlot({
    sub <- data
    sub <- subset(sub,sub$mal_tested=="1")
    drawGraph("pie",sub,factor(sub$mal_tested_pos),"Malaria Tests Done",NULL,"Malaria Tested Positive","Malaria")    
  })

drawGraph <- function (graphType,dataFrame,xVariable,xLabel,yVariable,yLabel,legendTitle,groupVariable) {
    #check graph type
    if(!is.null(xVariable)){
      if(is.factor(xVariable)){
        graphX <- factor(xVariable)
      }else{
        graphX <- as.factor(xVariable)
      }
      graphY <- yVariable
    }

 c <- ggplot(dataFrame, aes(x=factor(nrow(dataFrame)),fill=graphX),environment=environment()) + geom_bar(width = 1)
      print(c + coord_polar(theta = "y")+ xlab(xLabel)+ylab(yLabel)+ labs(fill=legendTitle)+ theme(
        panel.grid.minor = element_blank(),
        panel.grid.major = element_blank(),
        panel.background = element_blank(),
        plot.background = element_blank()
      ))

}

and in my ui.R

plotOutput("malPie", width="95%")

How do I get the plot to have a transparent background???

Tuti




Winston Chang

unread,
Jun 17, 2013, 1:21:15 PM6/17/13
to shiny-...@googlegroups.com
Hm, this may require some modification to renderPlot. It appears that the call to png() needs to have bg="transparent".

For example:

library(ggplot2)

trans_theme <- theme(
    panel.grid.minor = element_blank(), 
    panel.grid.major = element_blank(),
    panel.background = element_rect(fill=NA),
    plot.background = element_rect(fill=NA)
)

png("transparent.png", bg = "transparent")
qplot(1:3, 1:3) + trans_theme
dev.off()

png("white.png", bg = "white")
qplot(1:3, 1:3) + trans_theme
dev.off()


## Same behavior with CairoPNG
library(Cairo)
CairoPNG("transparent_cairopng.png", bg = "transparent")
qplot(1:3, 1:3) + trans_theme
dev.off()

CairoPNG("white_cairopng.png", bg = "white")
qplot(1:3, 1:3) + trans_theme
dev.off()


I've filed an issue about it at: https://github.com/rstudio/shiny/issues/178

-Winston







--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Winston Chang

unread,
Jun 17, 2013, 2:17:59 PM6/17/13
to shiny-...@googlegroups.com
Oops, I didn't realize earlier that renderPlot() already can pass arguments to png(). So all you need to do is something like this:

  output$plot <- renderPlot({
    print(qplot(1:3, 1:3) + 
      theme(
       panel.grid.minor = element_blank(), 
        panel.grid.major = element_blank(),
        panel.background = element_blank(),
        plot.background = element_blank()
      )
    )
  }, bg = "transparent")


-Winston


Reply all
Reply to author
Forward
0 new messages