Bar*_*art 3 r bar-chart ggplot2
我一直在尝试创建一个自定义条形图,在其中可以更改每组的颜色和图案(条纹或无条纹)。
structure(list(Level= c(0.2, 0.3, 0.25, 0.35, 0.4, 0.5, 0.5, 0.6, 0.15, 0.35), Group= c("A", "A",
"B", "B", "C", "C", "D", "D",
"E", "E"), Condition = c("no", "yes", "no", "yes", "no", "yes", "no",
"yes", "no", "yes")), row.names = c(NA, -10L), class = c("tbl_df",
"tbl", "data.frame"))
Run Code Online (Sandbox Code Playgroud)
然而,我希望这两个组都是白色的,并且如果条件 == yes 的条形中有黑色条纹,就像 ggpattern 图中经常看到的那样。
使用 ggpattern 我找不到可行的解决方案 [Error in seq.default(from, to, by) : invalid '(to - from)/by'] 是我最常遇到的错误之一。
p<- ggplot(z, aes(fill=Condition, y=Level, x=Group, pattern = Condition)) +
geom_bar(position="dodge", stat="identity") +
ggpubr::theme_pubr() +
theme(legend.position = "top") +
theme( panel.background = element_rect(colour = "black", size=0.5)) +
labs(x = "Group", y = "Level")+
scale_y_continuous(breaks=seq(0, 0.8, 0.1))+
scale_fill_discrete(name = "", labels = c("No", "Yes"))
Run Code Online (Sandbox Code Playgroud)
您在寻找这样的东西吗?
ggplot(z, aes(fill=Condition, y=Level, x=Group, pattern = Condition,
pattern_type = Condition)) +
geom_bar_pattern(position="dodge", stat="identity", pattern_fill = "black",
fill = "white", colour = "black", pattern_spacing = 0.01,
pattern_frequency = 5, pattern_angle = 45) +
ggpubr::theme_pubr() +
theme(legend.position = "top") +
theme( panel.background = element_rect(colour = "black", size=0.5)) +
labs(x = "Group", y = "Level")+
scale_y_continuous(breaks=seq(0, 0.8, 0.1), limits = c(0, 0.8)) +
scale_pattern_manual(values=c('stripe', 'none')) +
scale_pattern_type_manual(values=c(NA, NA))
Run Code Online (Sandbox Code Playgroud)