如何在保存图时保留图例中的(条纹)图案(R-ggplot-ggpattern)

aor9mmx1  于 2023-02-17  发布在  其他
关注(0)|答案(1)|浏览(91)

我无法将图表另存为图像(png或其他格式),图例中保留条纹图案。点图案和其他图案可以,但条纹不能。有人能给予我一些建议吗?提前感谢您的帮助
我的样品问题

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
)

图表如下所示:

lf5gs5x2

lf5gs5x21#

比例、宽度和高度的组合不合适:尝试比例小于1或调整宽度/高度:

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

相关问题