我在以下位置繪制了geom_line()和geom_hline()fordate和fb列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')

我使用 填充區域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?提前感謝您的幫助。
uj5u.com熱心網友回復:
geom_area是geom_ribbon從 0 開始的特殊情況。使用geom_ribbon并指定yminand 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')

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/435043.html
