I want to use ggplot2 & geom_bar to change bar color based on 1 factor variable and bar pattern based on another factor variable.
I can't figure out how to do this.
I use a Macbook Pro with Apple silicon, running R version 4.1.3 and RStudio 2022.02.1.
Any advice is greatly appreciated.
CodePudding user response:
Try something like this:
library(tidyverse)
library(ggpattern)
tribble(
~x, ~y, ~fill, ~pattern,
"a", 10, 1, 3,
"a", 15, 1, 3,
"a", 20, 1, 2,
"b", 10, 0, 2,
"b", 12, 0, 3,
"b", 13, 0, 3
) |>
mutate(fill = factor(fill),
pattern = factor(pattern)) |>
ggplot(aes(x, y, fill = fill))
geom_bar_pattern(aes(pattern = pattern), stat = "identity",
pattern_colour = "white")
Created on 2022-04-29 by the reprex package (v2.0.1)