Skip to content

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

@emstruong

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

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