How to keep the (stripe) pattern in the legend when saving a plot (R-ggplot-ggpattern)

363 Views Asked by At

I am not able to save the chart as an image (png or other) keeping the **stripe **pattern in the legend. Dot patterns and others work, but not stripes. Could anyone give me some advice? Thanks in advance for your help

My sample problem

library(tidyverse)
library(ggplot2)
library(ggpattern)

data <- data.frame(OBS_VALUE = rnorm(12, 10),
                   COUNTRY = c(rep(c("Flat"), times=6), rep(c("House"), times=6)),
                   SERIES_NAME = rep(c(rep(c("price"), times=3), rep(c("cost"), times=3)), times=2),
                   OBS_DATE = rep(seq(as.Date("2020-01-01"), as.Date("2022-01-01"), "years"), times=4)
)

chart = data %>%
  ggplot( aes(x=OBS_DATE, y=OBS_VALUE)) +
  geom_col_pattern( 
    aes(pattern = SERIES_NAME),
    position = position_dodge(),
    fill            = 'red',
    colour          = 'red', 
    pattern_density = 0.5, 
    pattern_fill    = 'white',
    pattern_colour  = 'white'
  )+
  scale_pattern_manual(values = c(price = "stripe", cost = "circle")) +
  facet_grid(~COUNTRY)
  


ggsave(
  "chart_test.png" ,
  plot = chart,
  scale = 1,
  width = 20,
  height = 10,
  units = "cm",
  dpi=320
)

And the chart appears like this: enter image description here

1

There are 1 best solutions below

0
On

It is the combination of scale and width and height which do not fit: Try a scale < 1 or adapt width / height:

ggsave(
  "chart_test.png" ,
  plot = chart,
  scale = 0.9,
  width = 20,
  height = 10,
  units = "cm",
  dpi=320
)

enter image description here