我用ggplot创建了一个线图,代码如下:
colors <- c("65% 2018" = "dodgerblue", "65% future" = "red") ggplot(dat, aes(x = year))+ geom_ribbon(aes(ymin = futuro_q1_65, ymax = futuro_q3_65), alpha = .3, fill = "red", color = "transparent") + geom_line(aes(y= futuro_media_65,color = "65% future"), lwd = .7) + geom_ribbon(aes(ymin = X2018_q1_65, ymax = X2018_q3_65), alpha = .3, fill = "dodgerblue", color = "transparent") + geom_line(aes(y = X2018_media_65, color = "65% 2018"), lwd = .7) + scale_x_continuous(limits=c(2019,2110))+ theme_classic()+ theme(axis.text=element_text(size=10), axis.title=element_text(size=10), legend.position="right")+ labs(x = "Year", y = "Evaporation", color = "water surface \nscenario") + scale_color_manual(values = colors)
结果图:
现在,我想通过指定我之前计算的箱图值,在x=2110处手动添加箱图。我在末尾插入方框图的代码:
colors <- c("65% 2018" = "dodgerblue", "65% future" = "red") ggplot(dat, aes(x = year))+ geom_ribbon(aes(ymin = futuro_q1_65, ymax = futuro_q3_65), alpha = .3, fill = "red", color = "transparent") + geom_line(aes(y= futuro_media_65,color = "65% future"), lwd = .7) + geom_ribbon(aes(ymin = X2018_q1_65, ymax = X2018_q3_65), alpha = .3, fill = "dodgerblue", color = "transparent") + geom_line(aes(y = X2018_media_65, color = "65% 2018"), lwd = .7) + scale_x_continuous(limits=c(2019,2110))+ theme_classic()+ theme(axis.text=element_text(size=10), axis.title=element_text(size=10), legend.position="right")+ labs(x = "Year", y = "Evaporation", color = "water surface \nscenario") + scale_color_manual(values = colors)+ geom_boxplot( stat = "identity", aes(x=2110, lower = 2312.8, upper = 2394.3, middle = 2343.5, ymin = 2254.8, ymax = 2440.4))
我收到以下错误:
geom_boxplot()
中出错:!将geom转换为grob时出现问题。ℹ 第5层发生错误。由draw_group()
中的错误引起:!每个组只能绘制一个方框图ℹ 你忘了aes(group = ...)
吗?
我尝试了geom_pointrange,它确实有效:
ggplot(dat, aes(x = year))+ geom_ribbon(aes(ymin = futuro_q1_65, ymax = futuro_q3_65), alpha = .3, fill = "red", color = "transparent") + geom_line(aes(y= futuro_media_65,color = "65% future"), lwd = .7) + geom_ribbon(aes(ymin = X2018_q1_65, ymax = X2018_q3_65), alpha = .3, fill = "dodgerblue", color = "transparent") + geom_line(aes(y = X2018_media_65, color = "65% 2018"), lwd = .7) + scale_x_continuous(limits=c(2019,2110))+ theme_classic()+ theme(axis.text=element_text(size=10), axis.title=element_text(size=10), legend.position="right")+ labs(x = "Year", y = "Evaporation", color = "water surface \nscenario") + scale_color_manual(values = colors)+ geom_pointrange(aes(x=2110,y=2343.5,ymax=2394.3,ymin=2312.8),col="red")
但这不是我想要的。如何获取箱图?