geom_ribbon
更喜欢连续的 x 值,但您可以通过在调用期间提供一个来欺骗它。
plt <- ggplot(dat, aes(x = Date)) +
geom_line(aes(y = Estimate, group = Area, color = Area)) +
geom_point(aes(y = Estimate, x = Date))
plt + geom_ribbon(aes(x = 1:length(Date), ymin = Lower, ymax = Upper), alpha = .2)
Additionally, you could use geom_linerange
, but that probably doesn't achieve the look you're going for:
plt + geom_linerange(aes(ymin = Lower, ymax = Upper, color = Area))
奇怪的是,用geom_ribbon
达到相同(有效)的结果(未显示图):
plt + geom_ribbon(aes(ymin = Lower, ymax = Upper, color = Area))
此外,您可以使用zoo
包将您的季度时间转换为可以理解为连续的时间,结合scale_x_yearqtr
:
library(zoo)
dat$Date <- as.yearqtr(dat$Date, format = 'Q%q_%y')
ggplot(dat, aes(x = Date)) +
scale_x_yearqtr(format = 'Q%q_%y') +
geom_line(aes(y = Estimate, group = Area, color = Area))+
geom_point(aes(y = Estimate, x = Date))+
geom_ribbon(aes(ymin = Lower, ymax = Upper), alpha = 0.2)