Last active
May 18, 2021 08:50
-
-
Save TanyaS08/001c8f61221db35db970e286b21dce9a to your computer and use it in GitHub Desktop.
Changing pattern and angle for {ggpattern}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(ggplot2) | |
library(dplyr) | |
library(palmerpenguins) | |
library(ggpattern) | |
penguins_summary <- | |
penguins %>% | |
group_by(species, sex) %>% | |
summarise_at(vars(flipper_length_mm), | |
mean) | |
ggplot() + | |
geom_col_pattern(data = penguins_summary %>% | |
na.omit(), | |
aes(y = flipper_length_mm, | |
x = sex, | |
#specify angle | |
pattern_angle = species, | |
# specify patter | |
pattern = species, | |
group = species, | |
#make new grouping variable with the species sex combos | |
fill = species), | |
pattern_fill = "black") + | |
#distinguish pattern type | |
scale_pattern_manual(values = c("crosshatch", "stripe", "stripe")) + | |
#distinguish pattern angle | |
scale_pattern_angle_manual(values = c(45, 45, 135)) + | |
#set colours | |
theme_bw() | |
## If you have a third grouping varible you can use that to specify | |
penguins_summary <- | |
penguins %>% | |
group_by(species, sex, island) %>% | |
summarise_at(vars(flipper_length_mm), | |
mean) | |
ggplot() + | |
geom_col_pattern(data = penguins_summary %>% | |
na.omit(), | |
aes(y = flipper_length_mm, | |
x = sex, | |
#specify angle | |
pattern_angle = island, | |
# specify pattern | |
pattern = island, | |
group = species, | |
fill = species), | |
pattern_fill = "black", | |
pattern_spacing = 0.03) + | |
#distinguish pattern type | |
scale_pattern_manual(values = c("crosshatch", "stripe", "stripe"), | |
guide = guide_legend(override.aes = list(pattern_spacing = 0.02, | |
fill = NA)) | |
) + | |
#prettier fill keys | |
scale_fill_manual(values = c("#ff4ccc", "#4c79ff", "#90ff4c"), | |
guide = guide_legend(override.aes = list(pattern = c('none', 'none', 'none'))) | |
) + | |
#distinguish pattern angle | |
scale_pattern_angle_manual(values = c(45, 45, 135)) + | |
#set colours | |
theme_bw() |
Author
TanyaS08
commented
May 18, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment