if (require("ggplot2")) {
# 'stripe' pattern example
df <- data.frame(level = c("a", "b", "c", 'd'), outcome = c(2.3, 1.9, 3.2, 1))
gg <- ggplot(df) +
geom_col_pattern(
aes(level, outcome, pattern_fill = level),
pattern = 'stripe',
fill = 'white',
colour = 'black'
) +
theme_bw(18) +
theme(legend.position = 'none') +
labs(
title = "ggpattern::geom_col_pattern()",
subtitle = "pattern = 'stripe'"
)
plot(gg)
# 'pch' pattern example
gg <- ggplot(mtcars, aes(as.factor(cyl), mpg)) +
geom_violin_pattern(aes(fill = as.factor(cyl),
pattern_shape = as.factor(cyl)),
pattern = 'pch',
pattern_density = 0.3,
pattern_angle = 0,
colour = 'black'
) +
theme_bw(18) +
theme(legend.position = 'none') +
labs(
title = "ggpattern::geom_violin_pattern()",
subtitle = "pattern = 'pch'"
)
plot(gg)
# 'polygon_tiling' pattern example
gg <- ggplot(mtcars) +
geom_density_pattern(
aes(
x = mpg,
pattern_fill = as.factor(cyl),
pattern_type = as.factor(cyl)
),
pattern = 'polygon_tiling',
pattern_key_scale_factor = 1.2
) +
scale_pattern_type_manual(values = c("hexagonal", "rhombille",
"pythagorean")) +
theme_bw(18) +
theme(legend.key.size = unit(2, 'cm')) +
labs(
title = "ggpattern::geom_density_pattern()",
subtitle = "pattern = 'polygon_tiling'"
)
plot(gg)
}
Run the code above in your browser using DataLab