Home > Software design >  ggpattern change pattern spacing without changing size
ggpattern change pattern spacing without changing size

Time:11-30

I want to create a figure using ggpattern to fill certain polygons with a dotted pattern, in a such a way that each polygon has a different density of dots. That is, I want the number of dots per area to be different between polygons, but still want all dots to be of the same size. Using the argumentpattern_spacing achieves the density goal, but changes the size of dots. How can I create a similar figure as the one below, but every dot has the same size?

ggplot2::ggplot(df, aes(trt, outcome))  
  ggpattern::geom_col_pattern(ggplot2::aes(fill = trt, 
                                           pattern_spacing = outcome/3),
                              pattern = 'pch')

enter image description here

CodePudding user response:

If you want to maintain the scale then the product of density and spacing must be kept constant

library(ggplot2)
library(ggpattern)

df <- data.frame(trt = c("A", "B", "C"), outcome = c(0.3, 0.9, 0.6))

ggplot(df, aes(trt, outcome))  
  geom_col_pattern(aes(fill = trt, pattern_density = trt,
                       pattern_spacing = trt), pattern = "pch",
                   color = "black")  
  scale_pattern_density_manual(values = c(0.4, 0.2, 0.1))  
  scale_pattern_spacing_manual(values = c(0.025, 0.05, 0.1))

Created on 2022-11-23 with reprex v2.0.2

  • Related