rstudio / thematic

Theme ggplot2, lattice, and base graphics based on a few simple settings.
https://rstudio.github.io/thematic/
Other
244 stars 10 forks source link

bg color isn't set correctly with thematic_with_theme() inside renderPlot() #52

Open MadhulikaTanuboddi opened 4 years ago

MadhulikaTanuboddi commented 4 years ago

Run the following example on RStudio Server Pro

library(shiny)
library(thematic)

theme_base <- thematic_theme("black", "white", accent = "purple")
theme_ggplot <- thematic_theme("black", "white", accent = "red")
theme_lattice <- thematic_theme("black", "white", accent = "orange")

ui <- fluidPage(
  plotOutput("base"),
  plotOutput("ggplot"),
  plotOutput("lattice")
)

server <- function(input, output, session) {

  output$base <- renderPlot({
    thematic_with_theme(theme_base, {
      plot(1:10, 1:10, col = thematic_get_option("accent"))
    })
  })

  output$ggplot <- renderPlot({
    thematic_with_theme(theme_ggplot, {
      ggplot2::qplot(1:10, 1:10, color = 1:10)
    })
  })

  output$lattice <- renderPlot({
    thematic_with_theme(theme_lattice, {
      lattice::show.settings()
    })
  })

}

shinyApp(ui, server)

Notice that base graph is not displayed as expected.

base_graph_colorado
cpsievert commented 4 years ago

Here's a more minimal example which makes the issue more obvious (the background color should be blue):

library(shiny)
library(thematic)

ui <- fluidPage(plotOutput("base"))

server <- function(input, output, session) {
  output$base <- renderPlot({
    thematic_with_theme(thematic_theme(bg = "blue", fg = "red"), {
      plot(1:10, 1:10)
    })
  })
}

shinyApp(ui, server)
Screen Shot 2020-08-03 at 1 09 18 PM

I'm not sure if this is something that makes sense for thematic to fix...the core issue here is that Cairo::CairoPNG() doesn't respect par(bg = ...), so there are at least 2 workarounds:

  1. Use a different graphics device than Cairo::CairoPNG(). That can be done with either options(shiny.usecairo = FALSE) or options(shiny.useragg = TRUE) (requires shiny v1.5).

  2. Set bg = thematic_get_option("bg") in renderPlot(), so for example:

renderPlot({
  thematic_with_theme(thematic_theme(), {
    plot(1:10, 1:10)
  })
}, bg = thematic_get_option("bg"))