[英]How to fill area under a geom_line() plot with ggplot?
I'm trying to fill the area below and above the two lines that are plotted through ggplot.我正在尝试填充通过 ggplot 绘制的两条线下方和上方的区域。 These are the data points that are plotted:这些是绘制的数据点:
polygon_data <- data.frame(checkpoints=c(650,1625,3250,650,1625,3250), lower=c(-1.17,0.20,1.36,-Inf,-Inf,-Inf), upper=c(2.28,1.75,1.36,Inf,Inf,Inf))
checkpoints_data <- data.frame(checkpoints=c(650,1625,3250),design.lower.bound=c(-1.17,0.20,1.36),design.upper.bound=c(2.28,1.75,1.36))
ggplot()+
geom_polygon(data=polygon_data,mapping=aes(x=checkpoints, y=lower, fill="red", alpha=0.8))+
geom_polygon(data=polygon_data,mapping=aes(x=checkpoints, y=upper, fill="green", alpha=0.8))+
geom_line(checkpoints_data,mapping= aes(x=checkpoints, y=design.upper.bound))+
geom_line(checkpoints_data,mapping= aes(x=checkpoints, y=design.lower.bound))+
geom_point(checkpoints_data,mapping= aes(x=checkpoints, y=design.lower.bound))+
geom_point(checkpoints_data,mapping= aes(x=checkpoints, y=design.upper.bound))
This is what I get instead of having the two areas completely filled.这就是我得到的,而不是完全填充两个区域。 Also colors are not respected by ggplot and I don't understand why: ggplot 也不尊重颜色,我不明白为什么:
plot image绘图图像
Thanks in advance for your help!在此先感谢您的帮助!
I think you have your coordinates incorrect in the polygon_data
data frame.我认为您在polygon_data
数据框中的坐标不正确。 c(650, 1625, 3250, 650, 1625, 3250)
should be c(650, 1625, 3250, 3250, 1625, 650)
c(650, 1625, 3250, 650, 1625, 3250)
应该是c(650, 1625, 3250, 3250, 1625, 650)
Try this:试试这个:
polygon_data <- data.frame(checkpoints = c(650, 1625, 3250, 3250, 1625, 650),
lower = c(-1.17, 0.20, 1.36, -Inf, -Inf, -Inf),
upper = c(2.28, 1.75, 1.36, Inf, Inf, Inf))
Maybe it is also a good idea to put the alpha
parameter outside aes()
.也许将alpha
参数放在aes()
之外也是一个好主意。 This avoids getting an extra legend.这避免了获得额外的图例。 The legend of fill
also looks a bit weird. fill
的传说看起来也有点怪怪的。 Maybe it is also a good idea to place fill
outside aes()
.也许将fill
放在aes()
之外也是一个好主意。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.