coolbutuseless / ggsvg

Use SVG images as ggplot points
https://coolbutuseless.github.io/package/ggsvg/
Other
138 stars 5 forks source link

Some scales not working #7

Closed coolbutuseless closed 2 years ago

coolbutuseless commented 2 years ago

For example scale_svg_fill_viridis_c is not working and complains about discrete/continuous values.

library(ggsvg)
library(ggplot2)

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Simple SVG of just a filled circle
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
svg_text <- '
  <svg viewBox="0 0 100 100 ">
    <circle class="pale" cx="50" cy="50" r="20" style="stroke-width:2; stroke:black;" fill="#aabbcc" />
  </svg>
  '

grid::grid.draw( svg_to_rasterGrob(svg_text) )

test_df <- data.frame(x=1:3, y=1:3, type=c('a', 'b', 'c'), stringsAsFactors = FALSE)
test_df

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# scale_svg_fill_continuous  WORKS
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ggplot(test_df) +
  geom_point_svg(
    aes(x, y, css("circle", fill = x)),
    svg = svg_text,
    size = 30
  ) +
  theme_bw() +
  scale_svg_fill_continuous(aesthetics = css("circle", fill = x))  # Works

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#  scale_svg_fill_viridis_c   FAILS!!!!
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ggplot(test_df) +
  geom_point_svg(
    aes(x, y, css("circle", fill = x)),
    svg = svg_text,
    size = 30
  ) +
  theme_bw() +
  scale_svg_fill_viridis_c(aesthetics = css("circle", fill = x))
coolbutuseless commented 2 years ago

Problem identified: I haven't included prepare_aestethics() in many of the custom SVG scales.

the prepare_aesthetics() function tidies up any css() style selectors into their canonical form for internal processing.

Since I fail to do that, ggplot gets confused.