项目:我目前正在尝试构建一个闪亮的应用程序,允许用户生成图表,他们可以修改检查的时间段,选择要按时间段和感兴趣的国家查看的变量。
我目前构建的应用程序没有任何问题,但我正在努力增加置信区间。我相信这是因为数据已从长格式转换为宽格式。请参阅下面的随机数据样本:
country_name year variable value variable_low value_low variable_high value_high
Uruguay 2002 v2x_delibdem 0.851 v2x_partipdem_codelow 0.724 v2x_partipdem_codehigh 0.796
Pakistan 2014 v2x_delibdem 0.248 v2x_egaldem_codelow 0.119 v2x_libdem_codehigh 0.312
Costa Rica 1992 v2x_delibdem 0.864 v2x_polyarchy_codelow 0.882 v2x_partipdem_codehigh 0.691
Botswana 2005 v2x_delibdem 0.527 v2x_libdem_codelow 0.518 v2x_libdem_codehigh 0.626
Brazil 1979 v2x_egaldem 0.116 v2x_partipdem_codelow 0.147 v2x_delibdem_codehigh 0.105
Uruguay 1975 v2x_egaldem 0.207 v2x_egaldem_codelow 0.178 v2x_polyarchy_codehigh 0.117
Niger 1970 v2x_libdem 0.154 v2x_egaldem_codelow 0.149 v2x_libdem_codehigh 0.176
Romania 2009 v2x_partipdem 0.478 v2x_partipdem_codelow 0.422 v2x_polyarchy_codehigh 0.717
Thailand 1997 v2x_partipdem 0.338 v2x_polyarchy_codelow 0.481 v2x_polyarchy_codehigh 0.617
Peru 1975 v2x_polyarchy 0.104 v2x_egaldem_codelow 0.076 v2x_partipdem_codehigh 0.078
value_low和value_high是variable_low和variable_high的熔化值。这些是置信区间的上限和下限。
当我运行这个ggplot2代码时:
myColors <- brewer.pal(5,"Set1")
names(myColors) <- levels(dat$variable)
colScale <- scale_colour_manual(name = "Variable",values = myColors)
ggplot(filter(dat, country_name == "Argentina", variable %in%c("v2x_delibdem","v2x_egaldem"))) +
geom_line(aes(x=year, y=value, group = variable, color = variable), size = 1)+
geom_point(aes(x=year, y=value, group = variable, color = variable), size = 3)+
geom_ribbon(aes(x=year, ymin=value_low, ymax=value_high, group = variable, color = variable), linetype=2, alpha=0.1)+
colScale +
scale_x_continuous(breaks=pretty_breaks(n=10),limits=c(1970,2015))+
scale_y_continuous("Index Score", breaks = scales::pretty_breaks(n = 10),limits=c(0, 1))+
theme(text = element_text(size=15))
显然我的方法不起作用。我想知道是否有人有经验在geom_line图表周围添加置信区间丝带,绘制多个变量。