不同图案和颜色的双向方差分析条形图

问题描述 投票:0回答:1

我在同一张图中绘制了两个因素和两个变量。但是,我希望因子时间不仅以不同的颜色显示每个条形,而且还以不同的模式显示(使用 ggpattern)。尽管如此,我只实现了添加颜色。这是数据集:

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"))

这里是我想要创建的代码和图形,但我没有设法正确添加模式:

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)

r ggplot2 statistics anova ggpattern
1个回答
0
投票

geom_bar
更改为
ggpattern::geom_bar_pattern
,并添加
aes(pattern_file=Time)

library(ggplot2)
library(ggpattern)
ggplot(example, aes(x=Type, y=value, fill=Time))+
  geom_bar_pattern(aes(pattern_fill=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)

© www.soinside.com 2019 - 2024. All rights reserved.