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)
另外,你可以使用geom_linerange
,但这可能无法实现你想要的外观:
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)