在ggplot2中设置geom_area()的上限和下限

发布于 2025-01-10 18:40:37 字数 2027 浏览 0 评论 0原文

我为 df 中的 datefb 列绘制了 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')

enter image description here

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')

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟花易冷人易散 2025-01-17 18:40:37

geom_areageom_ribbon 的特殊情况,从 0 开始。使用 geom_ribbon 并指定 yminy:

df1 %>% 
  ggplot(aes(x=as.POSIXct(date), y=fb)) + 
  geom_line(size=1, alpha=0.7, color='red') +
  geom_ribbon(aes(ymin = min(fb), ymax = fb), fill = 'red', alpha=0.3, position = "identity") +
  geom_hline(yintercept=c(min(df1$fb), max(df1$fb)), linetype='solid', col='black')

在此处输入图像描述

geom_area is a special case of geom_ribbon which starts at 0. Use geom_ribbon and specify ymin and y:

df1 %>% 
  ggplot(aes(x=as.POSIXct(date), y=fb)) + 
  geom_line(size=1, alpha=0.7, color='red') +
  geom_ribbon(aes(ymin = min(fb), ymax = fb), fill = 'red', alpha=0.3, position = "identity") +
  geom_hline(yintercept=c(min(df1$fb), max(df1$fb)), linetype='solid', col='black')

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文