thomasp85 / ggfx

Filters and Shaders for 'ggplot2'
https://ggfx.data-imaginist.com
Other
165 stars 4 forks source link

Error with custom filter and theme_void() #29

Open nrennie opened 10 months ago

nrennie commented 10 months ago

Adding theme_void() to a plot with a custom filter using Magick results in an error. When testing with other theme functions such as theme_minimal() or theme_classic() it works fine. Small reprex based on the example in the documentation:

library(ggplot2)
library(ggfx)

# Example from documentation
implode <- function(x, factor = 0.5) {
  vp <- magick::image_read(get_viewport_area(x))
  vp <- magick::image_implode(vp, factor = factor)
  set_viewport_area(x, as.raster(vp, native = TRUE))
}

ggplot(mtcars, aes(x = factor(gear), y = disp)) +
  with_custom(
    geom_boxplot(aes(fill = as.factor(gear))),
    filter = implode
  ) 


# Adding theme_void() returns an error
# Other theme_*() functions don't seem to have this issue
ggplot(mtcars, aes(x = factor(gear), y = disp)) +
  with_custom(
    geom_boxplot(aes(fill = as.factor(gear))),
    filter = implode
  ) +
  theme_void()
#> Error in .subset(raster, index): only 0's may be mixed with negative subscripts

Created on 2023-08-25 with reprex v2.0.2

MartinMSPedersen commented 10 months ago

You can define your own theme and can get the same effect as theme_void().

library(ggplot2)
library(ggfx)

# Example from documentation
implode <- function(x, factor = 0.5) {
  vp <- magick::image_read(get_viewport_area(x))
  vp <- magick::image_implode(vp, factor = factor)
  set_viewport_area(x, as.raster(vp, native = TRUE))
}

nothing <- 
    theme(
          plot.background = element_rect(fill = "#ffffff"),
          panel.background = element_rect(fill = "#ffffff"),
          panel.grid = element_blank(),
          axis.title = element_blank(),
          axis.text = element_blank(),
          axis.ticks = element_blank(),
          legend.position = "none"
    )

ggplot(mtcars, aes(x = factor(gear), y = disp)) +
  with_custom(
    geom_boxplot(aes(fill = as.factor(gear))),
    filter = implode
  ) + 
  nothing

nothing