wilkelab / cowplot

cowplot: Streamlined Plot Theme and Plot Annotations for ggplot2
https://wilkelab.org/cowplot/
704 stars 84 forks source link

Issue with importing svgs #119

Closed rraadd88 closed 5 years ago

rraadd88 commented 5 years ago

Hi, Description: I am trying to include some svgs (made in python) into a multi-panel figure using cowplot. First I tried the demo code to import tiger.svg, it works well. However, interestingly, there is a 'core dumped' error when I try to import my svg. Lot of memory is free and the svg I am trying to import is fairly small (zip). So, I wonder if there any bug or some types of svgs are incompatible.

Demo code works.

library(cowplot)
p <- ggplot(iris, aes(x = Sepal.Length, fill = Species)) + geom_density(alpha = 0.7)
p2 <- ggdraw() + draw_image("http://jeroen.github.io/images/tiger.svg", scale = 0.9)
plot_grid(p, p2, labels = "AUTO")

But not with my svg (zip).

library(cowplot)
p <- ggplot(iris, aes(x = Sepal.Length, fill = Species)) + geom_density(alpha = 0.7)
p2 <- ggdraw() + draw_image("test.svg", scale = 0.9)
plot_grid(p, p2, labels = "AUTO")

The issue is with the draw_image command.

draw_image("test.svg", scale = 1)
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc
Aborted (core dumped)

I am using R from anaconda environment on Ubuntu 16.04.

Also, additional information (may lead to possible solution),

I tried image_read_svg and it worked without error. So may be there is some issue with draw_image(?).

image_read_svg('test.svg', width = 400) 

Is there any workaround?

clauswilke commented 5 years ago

Can you try this?

draw_image(image_read_svg('test.svg', width = 400), scale = 1)

In any case, this is almost certainly a magick bug, not a cowplot bug. cowplot just dispatches to magick for image handling.

rraadd88 commented 5 years ago

Thank you for the quick response. I tried and it worked but the svg is rasterized. :/ screenshot from 2019-02-07 09-56-51

I wanted to import svgs as vector graphics just as in the demo. Anyways, since its an issue with the magick dependency, you may close the issue. If I find any fix/workaround, I'll post later. Thank you for your time.

clauswilke commented 5 years ago

Since this is using the magick library underneath, the image is getting rasterized one way or another. Better you rasterize explicitly, at a resolution that works for you.

y9c commented 5 years ago

Hi, @clauswilke,

Is it possible to embed svg image without rasterization, and keep the imported image in vector format?