如何使用ggplot2中的色带工具在两个geom之间填充h_lines
How to use the ribbon tool in ggplot2 to fill between two geom h_lines
所以我现在有带 hlines 的条形图来显示营养浓度的法定限制,但如果有意义的话,我希望这些限制显示为绘图后面的褪色块?
例如,对于 0.173 处的 h_line,我想要一个介于 0.069 和 0.173 之间的黄色带。这可能吗?如有请指教! - 我是 R
的新手
提前致谢!
#Primary phosphate
primary.P<-c(0.105,0.104,0.106,0.099,0.1,0.095)
groupP<-c("A","B","C","D","E","F")
sdP<-c(0.004,0.004,0.004,0.004,0.004,0.004)
p.p.dataframe<-data.frame(primary.P,groupP,sdP,stringsAsFactors=FALSE)
#the plot
ggplot(p.p.dataframe)+
geom_bar( aes(x=groupP, y=primary.P), stat="identity", fill="darkgrey", alpha=0.5) +
labs(y=expression(bold(Phosphate~"("*mg~N~L^-1*")")), x=expression(bold("Group")))+
geom_errorbar( aes(x=group, ymin=primary.P-sdP, ymax=primary.P+sdP), width=0.4, colour="orange", alpha=0.9, size=1.3)+
#adding ablines
geom_hline(yintercept = 0.173, col="yellow")+
geom_hline(yintercept = 0.069, col="")+
geom_hline(yintercept = 0.036, col="green")+
geom_hline(yintercept = 0.102, col="black", lwd=1, linetype=2)+
theme_classic()
试试这个。您可以通过 geom_rect:
添加黄色带
library(ggplot2)
primary.P<-c(0.105,0.104,0.106,0.099,0.1,0.095)
groupP<-c("A","B","C","D","E","F")
sdP<-c(0.004,0.004,0.004,0.004,0.004,0.004)
p.p.dataframe<-data.frame(primary.P,groupP,sdP,stringsAsFactors=FALSE)
#the plot
ggplot(p.p.dataframe)+
# Yellow band with transparency alpha = .1
# "dummy_yellow" is just a dummy name.
geom_rect(aes(fill = "dummy_yellow"), xmin = 0, xmax = 10, ymin = 0.069, ymax = 0.173, alpha = .1)+
geom_rect(aes(fill = "dummy_green"), xmin = 0, xmax = 10, ymin = 0, ymax = 0.036, alpha = .1)+
geom_bar(aes(x=groupP, y=primary.P), stat="identity", fill="darkgrey", alpha=0.5) +
labs(y=expression(bold(Phosphate~"("*mg~N~L^-1*")")), x=expression(bold("Group")))+
geom_errorbar( aes(x=groupP, ymin=primary.P-sdP, ymax=primary.P+sdP), width=0.4, colour="orange", alpha=0.9, size=1.3)+
#adding ablines
geom_hline(yintercept = 0.173, col="yellow")+
geom_hline(yintercept = 0.069, col="transparent")+
geom_hline(yintercept = 0.036, col="green")+
geom_hline(yintercept = 0.102, col="black", lwd=1, linetype=2) +
# Here we assign to dummy_yellow the fill color "yellow"
scale_fill_manual(name = "colored bands", values = c(dummy_yellow = "yellow", dummy_green = "green")) +
theme_classic()
由 reprex package (v0.3.0)
于 2020-03-31 创建
所以我现在有带 hlines 的条形图来显示营养浓度的法定限制,但如果有意义的话,我希望这些限制显示为绘图后面的褪色块? 例如,对于 0.173 处的 h_line,我想要一个介于 0.069 和 0.173 之间的黄色带。这可能吗?如有请指教! - 我是 R
的新手提前致谢!
#Primary phosphate
primary.P<-c(0.105,0.104,0.106,0.099,0.1,0.095)
groupP<-c("A","B","C","D","E","F")
sdP<-c(0.004,0.004,0.004,0.004,0.004,0.004)
p.p.dataframe<-data.frame(primary.P,groupP,sdP,stringsAsFactors=FALSE)
#the plot
ggplot(p.p.dataframe)+
geom_bar( aes(x=groupP, y=primary.P), stat="identity", fill="darkgrey", alpha=0.5) +
labs(y=expression(bold(Phosphate~"("*mg~N~L^-1*")")), x=expression(bold("Group")))+
geom_errorbar( aes(x=group, ymin=primary.P-sdP, ymax=primary.P+sdP), width=0.4, colour="orange", alpha=0.9, size=1.3)+
#adding ablines
geom_hline(yintercept = 0.173, col="yellow")+
geom_hline(yintercept = 0.069, col="")+
geom_hline(yintercept = 0.036, col="green")+
geom_hline(yintercept = 0.102, col="black", lwd=1, linetype=2)+
theme_classic()
试试这个。您可以通过 geom_rect:
添加黄色带library(ggplot2)
primary.P<-c(0.105,0.104,0.106,0.099,0.1,0.095)
groupP<-c("A","B","C","D","E","F")
sdP<-c(0.004,0.004,0.004,0.004,0.004,0.004)
p.p.dataframe<-data.frame(primary.P,groupP,sdP,stringsAsFactors=FALSE)
#the plot
ggplot(p.p.dataframe)+
# Yellow band with transparency alpha = .1
# "dummy_yellow" is just a dummy name.
geom_rect(aes(fill = "dummy_yellow"), xmin = 0, xmax = 10, ymin = 0.069, ymax = 0.173, alpha = .1)+
geom_rect(aes(fill = "dummy_green"), xmin = 0, xmax = 10, ymin = 0, ymax = 0.036, alpha = .1)+
geom_bar(aes(x=groupP, y=primary.P), stat="identity", fill="darkgrey", alpha=0.5) +
labs(y=expression(bold(Phosphate~"("*mg~N~L^-1*")")), x=expression(bold("Group")))+
geom_errorbar( aes(x=groupP, ymin=primary.P-sdP, ymax=primary.P+sdP), width=0.4, colour="orange", alpha=0.9, size=1.3)+
#adding ablines
geom_hline(yintercept = 0.173, col="yellow")+
geom_hline(yintercept = 0.069, col="transparent")+
geom_hline(yintercept = 0.036, col="green")+
geom_hline(yintercept = 0.102, col="black", lwd=1, linetype=2) +
# Here we assign to dummy_yellow the fill color "yellow"
scale_fill_manual(name = "colored bands", values = c(dummy_yellow = "yellow", dummy_green = "green")) +
theme_classic()
由 reprex package (v0.3.0)
于 2020-03-31 创建