我一直在嘗試創建一個自定義條形圖,我可以在其中更改每組的顏色和圖案(條紋或無條紋)。
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"))
我嘗試了以下創建下圖的方法。

但是,我希望這兩個組都為白色,并且如果條件 == 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"))
uj5u.com熱心網友回復:
你在尋找這樣的東西嗎?
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))

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/466722.html
下一篇:R:如何繪制不同顏色/形狀的點
