4

I want to hand-draw shapes in a software, import them into R and then draw them in ggplot2 with a specified fill/color/size. Basically, make a ggplot2 coloring book.

For example, if I wanted to draw this bird, I would like to be able to specify the fill/color and zie of the "beak", "head", "body", "tail" "left wing" "right wing" polygons and the "left leg", "right leg" and "eye" lines.

If this is doable, what file format/tools would be appropriate ? I've seen this stackoverflow question that imported an SVG file into R. Can I hand-draw SVG files? Maybe in GIMP?

enter image description here

Zoltan
  • 760
  • 4
  • 15
  • Zoltan, that is a brilliant idea, however, I am not sure that you can get this done the way you think. `{ggplot}` is a graphing library that places - let's say - "forms" on a coordinate system on the canvas. Thus, you would have to turn the drawing into a set of coordinates and link the points by lines/curves, or add circles, etc. With such forms or polygons, you can then utilise the fill parameter. There might be support with converting objects like your example into "points to connect" or respective geometrical shapes. Hope this helps your further search! – Ray Jun 05 '21 at 13:16
  • Thanks Ray for replying! I think what I'm hoping for is something that could take a hand-drawn shape (SVG?) then convert it to a list of points that can be connected using geom_polygon() I could then move it around on the ggplot by adding or substracting from the x/y values,. – Zoltan Jun 05 '21 at 13:32
  • The only reason I think it is possible is because it appears to be what they did in the stack overflow question I linked. – Zoltan Jun 05 '21 at 13:39
  • This looks like a partial solution https://stackoverflow.com/questions/61387217/use-svg-images-as-symbols-in-gglot2 – CSJCampbell Jun 05 '21 at 20:58
  • thanks -- I'll have a look . I'm not sure it allows to changing the fill color. – Zoltan Jun 06 '21 at 02:06
  • actually it might! https://cran.r-project.org/web/packages/grImport/vignettes/import.pdf , page 11 – Zoltan Jun 06 '21 at 02:10
  • I think I found something even easier. For the fill, I just just create a filled "black" version of each body part, then add them on ggplot using ggimage::geom_image(data = df, aes(x=x, y=y), image = "my_file.svg", color = "red", size =0.2) For the color, I create an outline of each body part, then add them in a similar way. This doesnt allow me to change the thickness of the line, but I could just create multiple svg . – Zoltan Jun 06 '21 at 03:12
  • Awesome. post an answer to your problem for others to follow in your footsteps. If tou are still looking for some tweaking, it appears that `{magick}`, https://docs.ropensci.org/magick/articles/intro.html, supports to fill pixels and their neighbours/surroundings. Check out the `#Paint shirt organge` example, `fill_image()` allows to set the pixel (you want to start with (could be any inner point of shape), the colour you like, and with `fuzz` yo.u control the level of neighbours to be filled. – Ray Jun 06 '21 at 07:51
  • yep I'm working on an answer with a nice regex. – Zoltan Jun 07 '21 at 11:20

0 Answers0