Home > Blockchain >  How do you suppress the ggdark warning message when using a function in lapply
How do you suppress the ggdark warning message when using a function in lapply

Time:08-25

I'm plotting my way through a list of data using pblapply. The plots use ggdark, which prints an error message each time it plots. It's messing up my progress bar for pblapply. I've read around and tried suppressWarnings(), but it doesn't work. Any ideas for how to suppress this warning message?

Inverted geom defaults of fill and color/colour.
To change them back, use invert_geom_defaults().

Here's a demo:

library(ggplot2)
library(pbapply)
library(ggdark)

# Make a list
p = split(diamonds, f=diamonds$cut)

# Open pdf
pdf("diamonds.pdf", onefile = T)

# Iterate through the list
pblapply(p, function(x) {
  
  print(
    ggplot(x)   
      geom_point(aes(carat, price, color = cut))   
      scale_y_continuous(label = scales::dollar)  
      guides(color = guide_legend(reverse = TRUE))  
      labs(title = "Prices of 50,000 round cut diamonds by carat and cut",
           x = "Weight (carats)",
           y = "Price in US dollars",
           color = "Quality of the cut")  
      dark_theme_bw()
  )
  
  # Revert to geom default
  invert_geom_defaults()
  
})

# Close pdf
dev.off()

CodePudding user response:

It's not a warning, it's a message. You need suppressMessages rather than suppressWarnings

pblapply(p, function(x) {
  
  print(
    suppressMessages(
    ggplot(x)   
      geom_point(aes(carat, price, color = cut))   
      scale_y_continuous(label = scales::dollar)  
      guides(color = guide_legend(reverse = TRUE))  
      labs(title = "Prices of 50,000 round cut diamonds by carat and cut",
           x = "Weight (carats)",
           y = "Price in US dollars",
           color = "Quality of the cut")  
      dark_theme_bw()
  )
  )
  
  # Revert to geom default
  invert_geom_defaults()
  
})

CodePudding user response:

Can you assign the plot to p, using theme_bw(), and then add dark_mode(verbose=F) to p?

pblapply(p, function(x) {
  
  p = ggplot(x)   
        geom_point(aes(carat, price, color = cut))   
        scale_y_continuous(label = scales::dollar)  
        guides(color = guide_legend(reverse = TRUE))  
        labs(title = "Prices of 50,000 round cut diamonds by carat and cut",
             x = "Weight (carats)",
             y = "Price in US dollars",
            color = "Quality of the cut")   
    theme_bw()
  
  print(p   dark_mode(verbose=F))
})
  • Related