1

Unresolved issue: There have been numerous posts about this with no solution. See R Shiny plot transparent background: low resolution (white pixels around axis text and labels, and title) and low resolution in R ggplot2 plots with transparent background on Shiny app

The problem: The problem occurs when one applies a transparent back ground color for a plot in shiny. This was done using bg = "transparent" in the renderPlot function. Any idea's for how to solve this are greatly appreciated!

This is what it looks like WITH the transparent background (just look at resolution of that text):

enter image description here

This is what it looks like WITHOUT the transparent background (it's perfect!): enter image description here

See here for a reproducible example:

library(shiny)
library(shinythemes)
library(readr)
library(tidyverse)

dat_in <- read_table2("term_year_fct   mean    value   Program name    subgroup_fct    Year
2017 7.647482014 139 STEMLearningCenter  AllUsers    AllUsers    1
2018   6.34741784  213 STEMLearningCenter  AllUsers    AllUsers    2
Summer&Fall2018 7.166666667 246 STEMLearningCenter  AllUsers    AllUsers    3
2019   7.759036145 249 STEMLearningCenter  AllUsers    AllUsers    4
Summer&Fall2019 11.97986577 149 STEMLearningCenter  AllUsers    AllUsers    5
2017 8.769230769 104 STEMLearningCenter  STEMUsers   STEMUsers   1
2018   8.563380282 142 STEMLearningCenter  STEMUsers   STEMUsers   2
Summer&Fall2018 9.51497006  167 STEMLearningCenter  STEMUsers   STEMUsers   3
2019   9.675824176 182 STEMLearningCenter  STEMUsers   STEMUsers   4
Summer&Fall2019 12.44680851 141 STEMLearningCenter  STEMUsers   STEMUsers   5
")



ui <- fluidPage(theme = shinytheme("superhero"),  # shinythemes::themeSelector(), #
                
                headerPanel("Test App"),
                sidebarLayout(
                  sidebarPanel(
                    uiOutput("choose_prog"),
                    
                    uiOutput("choose_name"),
                    br(),
                  ),
                  
                  mainPanel(
                    plotOutput("plot")
                  )
                )
                
)


server <- function(input, output) {
  # Drop down selection to chose the program 
  output$choose_prog <- renderUI({
    selectInput("program", "STEM Program", unique(dat_in$Program))
  })
  
  output$choose_name <- renderUI({

    
    # Get the program with the appropriate names
    req(input$program)
    dat <- subset(dat_in, Program == input$program)
    name_options <- unique(dat$name)
    
    # Create the check boxes & select them all by default 
    checkboxGroupInput("names", "select category of interest", 
                       choices = name_options,
                       selected = name_options)
    
  })

  
  output$plot <- renderPlot({
    # Output the plot
    req(input$program,input$names)
    dat <-subset(dat_in, Program %in% input$program & name %in% input$names)
    
    # head(dat)
    # head(dat)
    dat %>% 
      ggplot(aes(x=Year,y = value,fill = subgroup_fct)) + 
      geom_area(aes(colour = subgroup_fct, fill = subgroup_fct), color = "white", position = "identity") +
      ggtitle(unique(dat$Program))+ 
      scale_fill_manual(values = c("#285560", "#57a6b9", "#8fa13a")) +
      scale_y_continuous(limits = c(0, 300), expand = c(0, 20)) +
      scale_x_continuous(limits = c(1, 5), breaks = c(1:5),
                         labels = str_wrap(unique(dat$term_year_fct),
                                           width = 15)) + 
      xlab("") + 
      ylab("Count of \nUnique Users\n") + 
      
      geom_text(aes(label = ifelse(name == "All Users", value, "")), 
                hjust = 0.5, vjust = -1, show.legend = FALSE, color = "white")  +
      geom_text(aes(label = ifelse(name == "STEM Users", value, "")), 
                hjust = 0.5, vjust = 2, show.legend = FALSE) +
      geom_text(aes(label = ifelse(name == "Frequent Users", value, "")), 
                hjust = 0.5, vjust = 2, show.legend = FALSE) + 
      theme(
        # panel.background = element_blank(),  # Remove gray panel background
        panel.background = element_rect(fill = "transparent", colour = NA),
        plot.background = element_rect(fill = "transparent", colour = NA),
        text = element_text(size = 12, family = "Calibri"),  # Specify text size
        plot.title = element_text(hjust = 0.5,size = 20, vjust = 2.5, face = "bold", color ="white"),  # Center the title
        plot.subtitle = element_text(hjust = 0.5, size = 18, color ="white"),  # Center text
        axis.title.y = element_text(angle = 0, vjust = 0.5, margin = margin(r = 10), size = 15, face = "bold", color ="white"), 
        axis.ticks = element_blank(),  # Remove ticks
        axis.text.x = element_text(size = 15, hjust = 0.5, family = "Calibri", face = "bold", color ="white"),
        axis.text.y = element_text(size = 15, hjust = 0.5, family = "Calibri", face = "bold", color ="white"),# Set labels at 45 degree angle
        legend.title = element_blank(),  # Remove legend title
        legend.key = element_rect(fill = NA),
        legend.text=element_text(size=14),
        legend.position = "bottom",
        plot.margin=unit(c(1,1,1.5,1.2),"cm")) # Remove gray fill for legend keys
    
    
  }, bg = "transparent")
    
    
}  

shinyApp(ui = ui, server = server)
Brigadeiro
  • 2,649
  • 13
  • 30
NewBee
  • 990
  • 1
  • 7
  • 26

1 Answers1

1

Finally figured it out: add:

 type = "cairo"

to the renderPlot() call.

like so:

renderPlot({},bg = "transparent", type = "cairo")