Skip to content

Saving svg plots using ggsave() makes geom_rect disappear, but not with svg() #5121

Closed as not planned

Description

@emstruong

Description

When I save plots with geom_rect() using either ggsave() or svglite::svglite() the rectangles disappear, but not when I use
grDevices::svg() or png formats.

Code

``` r
library(ggplot2)
library(svglite)

set.seed(123)

df <-
  data.frame(
    x = rpois(300, 3),
    y = c(rbinom(100, 1, .3), rbinom(100, 1, .5), rbinom(100, 1, .7)),
    group = factor(rep(c("a", "b", "c"), each = 100)) 
  )

(loghistGraph<-ggplot(df, aes(xmin = x, xmax = x, ymin = rep(0, nrow(df)), ymax = rep(nrow(df)), color = group)) +
    geom_rect(size = 4, position = position_dodge(.3))
)

ggsave(plot = loghistGraph, file = "bugGraph.svg") 
#> Saving 7 x 5 in image
ggsave(plot= loghistGraph, file = "OkayGraph.png")
#> Saving 7 x 5 in image

# Works
grDevices::svg("DefaultSvgWorks.svg")
print(loghistGraph)
dev.off()
#> png 
#>   2

# Doesn't work and save as ggsave
svglite("svgliteBug.svg")
print(loghistGraph)
dev.off()
#> png 
#>   2

Created on 2022-12-26 by the reprex package (v2.0.1)

Attached are the graphs that did and did not work.

Using ggsave for .svg
bugGraph

Using svg()
DefaultSvgWorks

Using ggsave for .png
OkayGraph

Using svglite for .svg
svgliteBug

Activity

  1. teunbrand commented on Dec 27, 2022

    @teunbrand
    Collaborator

    As far as I can tell, {svglite} exports the rectangles as 'rect's, while svg() exports them as 'path's. The rectangles are still there in the text output of {svglite}, but I think they might not get rendered because they have 0-width.

    From the SVG docs here:

    The width and height properties define the overall width and height of the rectangle. A negative value for either property is illegal and must be ignored as a parsing error. A computed value of zero for either dimension disables rendering of the element.

    So I think everything is working as it should; it is just a difference in interpretation of what output to the file is (a 'rect' vs 'path'). In any case, I don't think ggplot2 has any control over this implementation.

  2. added a commit that references this issue on Jan 7, 2023
    0e08dfc
  3. teunbrand commented on Jan 10, 2023

    @teunbrand
    Collaborator

    Giving this another thought, I don't think there is anything to fix here at ggplot2's side, as this relates to how graphics are translated between R and devices. If you do insist on this plot working, either give the rectangles a small width or use a line to represent the data. Despite that this is unlikely to change in ggplot2, I do want to thank you for preparing the examples, and hope to hear from you again when you find new issues.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions