在ggplot2中设置geom_area()的上限和下限
我为 df
中的 date
和 fb
列绘制了 geom_line()
和 geom_hline()
>:
df <- structure(list(date = c("2015-5-1", "2015-6-1", "2015-7-1", "2015-8-1",
"2015-9-1", "2015-10-1", "2015-11-1", "2015-12-1", "2016-1-1",
"2016-2-1", "2016-3-1", "2016-4-1", "2016-5-1", "2016-6-1", "2016-7-1",
"2016-8-1", "2016-9-1", "2016-10-1"), fb = c(8.055790592, 8.032818097,
8.005365822, 7.985498773, 7.959200396, 7.919847621, 7.877668588,
7.835635821, 7.804472389, 7.788280201, 7.772438537, 7.750286446,
7.720577645, 7.699622927, 7.679943452, 7.671596434, 7.678374461,
7.677040934), ab = c(14.79734541, 14.66345424, 14.52954117, 14.39551705,
14.2613928, 14.12742798, 13.99331597, 13.85911943, 13.7250579,
13.59078964, 13.45659479, 13.32220287, 13.18777806, 13.05347518,
12.91905494, 12.78450571, 12.65039408, 12.51588272)), class = "data.frame", row.names = c(NA,
-18L))
df1 <- df %>%
mutate_at(vars(-date), funs(./100))
df1 %>%
ggplot(aes(x=as.POSIXct(date), y=fb)) +
geom_line(size=1, alpha=0.7, color='red') +
# geom_area(fill='red', alpha=0.3, position = "identity") +
geom_hline(yintercept=c(min(df1$fb), max(df1$fb)), linetype='solid', col='black')
我使用geom_area(fill='red', alpha=0.3,position = "identity")
填充了区域,但是你可能会注意到,该区域从开始>df1$fb == 0
会生成一个大的无效区域(如下图蓝色矩形中的区域)。
df1 %>%
ggplot(aes(x=as.POSIXct(date), y=fb)) +
geom_line(size=1, alpha=0.7, color='red') +
geom_area(fill='red', alpha=0.3, position = "identity") +
geom_hline(yintercept=c(min(df1$fb), max(df1$fb)), linetype='solid', col='black')
所以我的问题是我们如何才能只填充 fb
的最小值和最大值之间的下划线区域?感谢您提前的帮助。
I plotted geom_line()
and geom_hline()
for date
and fb
columns in df
:
df <- structure(list(date = c("2015-5-1", "2015-6-1", "2015-7-1", "2015-8-1",
"2015-9-1", "2015-10-1", "2015-11-1", "2015-12-1", "2016-1-1",
"2016-2-1", "2016-3-1", "2016-4-1", "2016-5-1", "2016-6-1", "2016-7-1",
"2016-8-1", "2016-9-1", "2016-10-1"), fb = c(8.055790592, 8.032818097,
8.005365822, 7.985498773, 7.959200396, 7.919847621, 7.877668588,
7.835635821, 7.804472389, 7.788280201, 7.772438537, 7.750286446,
7.720577645, 7.699622927, 7.679943452, 7.671596434, 7.678374461,
7.677040934), ab = c(14.79734541, 14.66345424, 14.52954117, 14.39551705,
14.2613928, 14.12742798, 13.99331597, 13.85911943, 13.7250579,
13.59078964, 13.45659479, 13.32220287, 13.18777806, 13.05347518,
12.91905494, 12.78450571, 12.65039408, 12.51588272)), class = "data.frame", row.names = c(NA,
-18L))
df1 <- df %>%
mutate_at(vars(-date), funs(./100))
df1 %>%
ggplot(aes(x=as.POSIXct(date), y=fb)) +
geom_line(size=1, alpha=0.7, color='red') +
# geom_area(fill='red', alpha=0.3, position = "identity") +
geom_hline(yintercept=c(min(df1$fb), max(df1$fb)), linetype='solid', col='black')
I filled area using geom_area(fill='red', alpha=0.3, position = "identity")
, but as you may notice, the area starting from df1$fb == 0
which generate a large futile areas (areas in the blue rectangle as shown in the figure below).
df1 %>%
ggplot(aes(x=as.POSIXct(date), y=fb)) +
geom_line(size=1, alpha=0.7, color='red') +
geom_area(fill='red', alpha=0.3, position = "identity") +
geom_hline(yintercept=c(min(df1$fb), max(df1$fb)), linetype='solid', col='black')
So my question is how could we only fill the underline area between minimum and maximum values of fb
? Thanks for your help at advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
geom_area
是geom_ribbon
的特殊情况,从 0 开始。使用geom_ribbon
并指定ymin
和y
:geom_area
is a special case ofgeom_ribbon
which starts at 0. Usegeom_ribbon
and specifyymin
andy
: