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.
CodePudding user response:
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')