I´m plotting two factors and two variables in the same graph. However, I want that the factor time not only appears each bar with a different color, but also with a different pattern (using ggpattern). Nonetheless, I only achieved adding the color. Here is the dataset:
dput(example)
structure(list(Type = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L,
3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 1L, 1L, 1L, 2L, 2L,
2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L), levels = c("A",
"B", "C", "D", "E", "F"), class = "factor"), value = c(5, 5.3,
5.5, 2, 1.9, 3, 4, 4.5, 6, 8.1, 9, 10, 7, 7.5, 9, 12, 13.5, 16,
3.5, 3.8, 4, 0.5, 0.4, 1.5, 2.5, 3, 4.5, 6.6, 7.5, 8.5, 5.5,
6, 7.5, 10.5, 12, 14.5), response = structure(c(1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L
), levels = c("a", "b"), class = "factor"), sd = c(0.895764470633308,
0.289362716931399, 0.104678169231026, 0.00930353935514927, 0.753359375020964,
0.638338206893093, 0.579651408510526, 0.964390131543651, 0.341300112732967,
0.76228717509891, 0.0845759617815788, 0.273269105643979, 0.857765282025983,
0.260972208876609, 0.805925034184099, 0.687981747137384, 0.989258579321479,
0.743281342584998, 0.168238567687899, 0.588839679785241, 0.0898999138186304,
0.0707352006427383, 0.808880144480776, 0.806555126114646, 0.0194885784995568,
0.800068228835156, 0.0935771768502066, 0.625519315733491, 0.946960588926455,
0.177769938144293, 0.532128866484254, 0.014955121437948, 0.03144820760672,
0.455614442351514, 0.975913745705029, 0.420061058948122), Time = structure(c(1L,
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L,
3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L,
1L, 2L, 3L), levels = c("10", "20", "30"), class = "factor")), row.names = c(NA,
-36L), class = c("tbl_df", "tbl", "data.frame"))
And here the code and figure I want to create, but I didn´t manage to add the patter properly:
library(ggplot2)
library(ggpubr)
library(tidyverse)
library(ggthemes)
library(ggpattern)
library(egg)
library(multcompView)
example
example$Type<- as.factor(example$Type)
example$response<- as.factor(example$response)
example$Time<- as.factor(example$Time)
plot <- ggplot(example, aes(x=Type, y=value, fill=Time))+
geom_bar(stat="identity", position="dodge", color="black",alpha = 1, width=0.8)+
geom_errorbar(aes(ymax=value+sd, ymin=value-sd), position=position_dodge(0.8), width=0.25, color="black", alpha=0.5, show.legend = FALSE)+
scale_fill_brewer(name="Time / min", palette = "Greens", guide = guide_legend(label.hjust = 0))+
theme_bw(base_size = 20) +
theme( panel.grid.major=element_blank(), panel.grid.minor=element_blank(),axis.text=element_text(size=20), axis.title=element_text(size=22,face="bold"), legend.title=element_text(size=14, face="bold"),legend.text=element_text(size=12), axis.text.x=element_text(angle = 45, hjust = 1),legend.position = c(0.94, 0.80) )+
labs(y = "Value", x= "")+
scale_y_continuous(expand = c(0, 0),limits=c(0, 25))+
facet_grid(.~response,labeller = label_both)
tag_facet(plot, fontface = 'bold', tag_pool = c("(a)","(b)"),
open = NULL, close = NULL, hjust = -0.05, size = 6)
Change from geom_bar
to ggpattern::geom_bar_pattern
, add aes(pattern=Time)
, and add scale_pattern_discrete
(to set the legend name).
library(ggplot2)
library(ggpattern)
ggplot(example, aes(x=Type, y=value, fill=Time))+
ggpattern::geom_bar_pattern(aes(pattern = Time), stat="identity", position="dodge", color="black",alpha = 1, width=0.8) +
geom_errorbar(aes(ymax=value+sd, ymin=value-sd), position=position_dodge(0.8), width=0.25, color="black", alpha=0.5, show.legend = FALSE)+
scale_fill_brewer(name="Time / min", palette = "Greens", guide = guide_legend(label.hjust = 0))+
theme_bw(base_size = 20) +
theme( panel.grid.major=element_blank(), panel.grid.minor=element_blank(),axis.text=element_text(size=20), axis.title=element_text(size=22,face="bold"), legend.title=element_text(size=14, face="bold"),legend.text=element_text(size=12), axis.text.x=element_text(angle = 45, hjust = 1),legend.position = c(0.94, 0.80) )+
labs(y = "Value", x= "")+
scale_y_continuous(expand = c(0, 0),limits=c(0, 25))+
facet_grid(.~response,labeller = label_both) +
ggpattern::scale_pattern_discrete(name="Time / min")
Note that if you want to include other pattern_*
aesthetics, you'll need to add scale_pattern*_discrete(name="Time / min")
to make sure the legends combine correctly.