如何为使用ggplot2

时间:2019-07-08 14:59:31

标签: r ggplot2

我正在尝试创建一个图表,该图表显示基于两个排放情景的未来湿地计数分布的小提琴图,并且在给定位置的两个小提琴图之间,我添加了一个带有标准误差线的点,用于显示历史湿地数数字。我遇到的问题是,图例仅显示了两种排放情况,而没有为历史数据创建带有标准误差线的点。

我试图将两个数据帧合并为一个,但是将来的池塘计数是1024个观测值,而历史记录只有16个观测值,因此我无法绑定它们,因为它们的长度是分开的,并且它们具有不同的列数阻止我将它们绑在一起。

 head(Historical_Ponds)
    Station  N     Ponds        sd        se       ci
1   Academy 27 327848.08 169503.19 32620.905 67053.23
2    Algona 27 162127.08  67165.60 12926.025 26569.83
3 Bottineau 27 468644.95 251768.16 48452.805 99596.17
4 Brookings 27 162127.08  67165.60 12926.025 26569.83
5     Clark 27 162127.08  67165.60 12926.025 26569.83
6 Crookston 27  39196.28  32141.79  6185.691 12714.87

str(Historical_Ponds)
'data.frame':   16 obs. of  6 variables:
 $ Station: chr  "Academy" "Algona" "Bottineau" "Brookings" ...
 $ N      : num  27 27 27 27 27 27 27 27 27 27 ...
 $ Ponds  : num  327848 162127 468645 162127 162127 ...
 $ sd     : num  169503 67166 251768 67166 67166 ...
 $ se     : num  32621 12926 48453 12926 12926 ...
 $ ci     : num  67053 26570 99596 26570 26570 ...

head(Future_Ponds)
     Ponds   Type Model Station
1 284750.4 RCP4.5     1 Academy
2 304635.6 RCP4.5     2 Academy
3 309434.9 RCP4.5     3 Academy
4 311087.2 RCP4.5     4 Academy
5 298294.1 RCP4.5     5 Academy
6 289198.0 RCP4.5     6 Academy

> str(Future_Ponds)
'data.frame':   1024 obs. of  4 variables:
 $ Ponds  : num  284750 304636 309435 311087 298294 ...
 $ Type   : Factor w/ 2 levels "RCP4.5","RCP8.5": 1 1 1 1 1 1 1 1 1 1 ...
 $ Model  : Factor w/ 32 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10 ...
 $ Station: Factor w/ 16 levels "WebsterCity",..: 3 3 3 3 3 3 3 3 3 3 ...

ggplot()+
  geom_violin(data=Future_Ponds,aes(x=Station, y=Ponds, fill=Type),adjust=2)+
  geom_point(data=Historical_Ponds, aes(x=Station, y=Ponds))+
  geom_errorbar(data=Historical_Ponds,aes(x=Station, ymin=Ponds-se,ymax=Ponds+se),width=.1)+
  labs(x="Location",y="May Pond Count",title="May Pond Count from (1990-2019) to (2070-2099)")+
  theme(legend.title=element_blank(),legend.text=element_text(size=8),legend.position="bottom",axis.text.x = element_text(size=8),axis.title.y=element_blank())+
  scale_fill_manual(values=c("blue","red"))+
  coord_flip()+
  scale_y_continuous(labels=scales::comma)

This is an image of the current graph the code will create, as you can see the legend only includes the emissions scenario and not the historical point with error bars

0 个答案:

没有答案