简体   繁体   English

如何使用 ggplot 填充 geom_line() 图下的区域?

[英]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.

相关问题 如何使用geom_rect在ggplot中填充geom_line图下方的区域? - How to fill area bellow geom_line plot in ggplot with geom_rect? ggplot:如何在三个不同的 geom_line() 下应用 geom_area() 或类似的填充区域而不重叠每个独立填充? - ggplot: How to apply geom_area() or similar to fill area under three distinct geom_line() without overlapping each indepedent fill? 如何“填充”从ggplot2 / R中的模型预测的geom_line下方的区域? - How to "fill" area below geom_line predicted from model in ggplot2 / R? 如何将 plot geom_line 特征转换为 ggplot2 map? - How to plot geom_line features in ggplot2 map? 如何在ggplot中为geom_line绘图添加点数 - how can i add points to geom_line plot in ggplot plot 中的垂直偏移 - ggplot 和 geom_line - vertical offset in plot - ggplot and geom_line ggplot使用geom_line()在绘图中创建伪像 - ggplot creates artefacts in plot with geom_line() 多行 plot 与 ggplot,geom_line() - multiple lines plot with ggplot, geom_line() 如何使用ggplot2填充values(geom_line)和拦截之间的空间? 截距上下截取的颜色不同 - How can I fill the space between values(geom_line) and an intercept with ggplot2? Different Colors for values over and under intercept 如何在 ggplot 中混合 geom_rect 和 geom_line 对一个区域进行着色? - How to shade an area in ggplot mixing geom_rect and geom_line?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM