Skip to content

Instantly share code, notes, and snippets.

@luisDVA
Created July 30, 2022 13:46
Show Gist options
  • Save luisDVA/0fb7dea571a1bc8416d7df6a74a323ac to your computer and use it in GitHub Desktop.
Save luisDVA/0fb7dea571a1bc8416d7df6a74a323ac to your computer and use it in GitHub Desktop.
can't seem to change fill color in geom_point_svg
library(ggsvg)
library(ggplot2)
# the svg that I need to use
svg_url <- "https://raw.githubusercontent.com/luisDVA/luisdva.github.io/master/assets/images/Cat-green.svg"
cat_svg <- paste(readLines(svg_url), collapse = "\n")
# toy data
dat <-
expand.grid(y=1:2,x=seq_len(2))
dat$qual <- letters[1:4]
dat
# works and uses default colors
ggplot(dat)+
geom_point_svg(aes(x, y,css(selector = "path:nth-child(1)", fill = qual)), svg = cat_svg) +
scale_svg_default()
# all colors are unchanged
ggplot(dat)+
geom_point_svg(aes(x, y,css(selector = "path:nth-child(1)", fill = qual)), svg = cat_svg) +
scale_svg_fill_viridis_d(aesthetics="fill")
# colors unchanged
ggplot(dat)+
geom_point_svg(aes(x, y,css(selector = "path:nth-child(1)", fill = qual)), svg = cat_svg) +
scale_svg_fill_viridis_d("path:nth-child(1)")
@coolbutuseless
Copy link

coolbutuseless commented Jul 30, 2022

Try:

ggplot(dat)+
  geom_point_svg(aes(x, y,css(selector = "path:nth-child(1)", fill = qual)), svg = cat_svg) +
  scale_svg_fill_viridis_d(aesthetics = css(selector = "path:nth-child(1)", fill = qual))

Explanation: The name of the aesthetic to the scale argument must match the aesthetic in the geom() call exactly.

@luisDVA
Copy link
Author

luisDVA commented Aug 1, 2022

thanks!!!!!!!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment