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

Error when ggplot object contains a theming command #132

Open rishabhshah-92 opened 1 year ago

rishabhshah-92 commented 1 year ago

Describe the problem

The following code works fine:

mtcars %>% ggplot(aes(wt,mpg,col=factor(cyl)))+geom_point()

... but adding a theming command causes an error:

mtcars %>% ggplot(aes(wt,mpg,col=factor(cyl)))+geom_point()+theme(legend.position = "none")

Error in vapply(ggplot2::get_element_tree(), function(x) { : 
  values must be length 1,
 but FUN(X[[10]]) result is length 2

This is the preamble I am using:

library(ggplot2)
library(thematic)
thematic_on()

Session Info

My ggplot2 version is 3.4.0 and thematic version is 0.1.2.9000. I am running R version 4.1.2 (2021-11-01), RStudio 2022.02.3 on macOS 12.2.1

cpsievert commented 1 year ago

Hmm, I'm not able to replicate this with R 4.2.2...would you mind double-checking that you can replicate from a fresh R session (without any other packages loaded)?

TearsRfuel commented 3 weeks ago

Same issue, in Rshiny.

library(shiny)
library(ggplot2)
library(thematic)
library(ragg)

thematic_shiny()

ui <- fluidPage(
  # bslib makes it easy to customize CSS styles for things
  # rendered by the browser, like tabsetPanel()
  # https://rstudio.github.io/bslib
  theme = bslib::bs_theme(
    bg = "#002B36", fg = "#EEE8D5", primary = "#2AA198",
    # bslib also makes it easy to import CSS fonts
    base_font = bslib::font_google("Pacifico")
  ),
  tabsetPanel(
    type = "pills",
    tabPanel("ggplot", plotOutput("ggplot")),
    tabPanel("lattice", plotOutput("lattice")),
    tabPanel("base", plotOutput("base"))
  ),
  nav_item(
    input_dark_mode(id = "mode", mode = 'light')
  )
)

server <- function(input, output) {

  output$ggplot <- renderPlot({
    ggplot(mtcars, aes(wt, mpg, label = rownames(mtcars), color = factor(cyl))) +
      theme_clean() + 
      geom_point() +
      ggrepel::geom_text_repel()
  })
  output$lattice <- renderPlot({
    lattice::show.settings()
  })
  output$base <- renderPlot({
    image(volcano, col = thematic_get_option("sequential"))
  })
}

shinyApp(ui, server)

Added a theme to the ggplot, and it doesn't auto-theme anymore.