2016-09-20 73 views
4

我有一個數據集,該數據集按年度映射某種自然現象的發生率,以顯示其隨時間增加。這些數據是:R:在條形圖上虛線「預測」在實心條上方

Year Value 
2012 10 
2013 45 
2014 212 
2015 560 
2016 570 

然而,2016年基於不完整的數據(最多隻能到2016年七月底)。因此,我想基本上是一個正常的條形圖,但2016年的「預測」完整價值點在上面,這是堅實的條形圖。我使用GGPLOT2爲基本圖形,這是我到目前爲止有:

test_DF = data.frame(Year = c("2012", "2013", "2014", "2015", "2016"), 
        Count = c(10, 45, 212, 560, 570)) 

base_graph = ggplot(test_DF, aes(x = test_DF$Year, y = test_DF$Count), 
        main = "Growth of Natural Phenomenon", xlab = "Year", 
        ylab = "Number of Occurences") 

final_growth = base_graph + geom_bar(stat = 'identity', fill="#4F81BD") + theme_few() + labs(title="Growth of Natural Phenomenon", y="Number of Occurences",x="") + 
    geom_text(aes(label=test_DF$Count, vjust=-0.4)) + ylim(0, 1000) 

但我不知道如何添加點/否則質感的「預測」部分高於最終欄 - 我可以Google找不到任何類似的東西。我想該圖是這樣的:

The graph I would like to make - apologies for the bad drawing, but hopefully gets the idea across

預先感謝任何建議!

回答

2

我認爲這是處理您的任務的一種方法。由於您希望在2016年爲預測的數據部分分配不同的顏色,因此我創建了一個組變量。您仍然需要清理圖例,但我認爲這是您可以處理的。

library(tidyverse) 

foo %>% 
add_row(Year = 2016, Value = 230) %>% 
mutate(group = c("a", "a", "a", "a", "a", "b")) -> out 


ggplot(data = out, aes(x = Year, y = Value, fill = group)) + 
geom_bar(stat = "identity") + 
scale_fill_manual(values = c("#4F81BD", "red")) + 
labs(title = "Growth of Natural Phenomenon", x = "Year", 
    y = "Number of Occurrences") 

enter image description here

DATA

foo <- structure(list(Year = 2012:2016, Value = c(10L, 45L, 212L, 560L, 
570L)), .Names = c("Year", "Value"), class = "data.frame", row.names = c(NA, 
-5L)) 
2

似乎是一個線圖將工作做好的位置:

library(ggplot2) 
library(ggthemes) 

test_DF = data.frame(Year = c(2015:2016, 2015:2016, 2012:2015), 
        Count = c(560, 570 + 230, 560, 570, 10, 45, 212, 560), 
        group=rep(c("predicted","measured: incomplete", "measured: complete"), 
           c(2,2,4))) 

test_DF$group = factor(test_DF$group, levels=c("predicted", "measured: incomplete","measured: complete")) 

ggplot(test_DF, aes(Year, Count, colour=group, linetype=group, shape=group)) + 
    geom_line(size=1) + 
    geom_point(size=2.5, stroke=1, fill="white") + 
    theme_few() + labs(colour="", linetype="", shape="") + 
    scale_colour_manual(values=hcl(c(15, 195,195),100,65)) + 
    scale_linetype_manual(values=c(2,3,1)) + 
    scale_shape_manual(values=c(16,21,16)) + 
    theme(legend.key.width=unit(2,"cm")) + 
    coord_cartesian(xlim=range(test_DF$Year) + c(-0.2,0.2), 
        ylim=c(0, max(test_DF$Count)*1.04), expand=FALSE) 

enter image description here

或者,用文本值,而不是點標記:

ggplot(test_DF, aes(Year, Count, colour=group, linetype=group, shape=group)) + 
    geom_line(size=0.5, alpha=0.5) + 
    geom_text(aes(label=ifelse(Year==2015 & group != "measured: complete", NA, Count)), 
      size=3.8, fontface="bold", show.legend=FALSE) + 
    theme_few() + labs(colour="", linetype="") + 
    scale_colour_manual(values=hcl(seq(15,375,length=4)[1:3],100,65)) + 
    scale_linetype_manual(values=c(2,3,1)) + 
    theme(legend.key.width=unit(1.5,"cm")) + 
    guides(colour=guide_legend(override.aes=list(alpha=1, size=0.8))) + 
    coord_cartesian(xlim=range(test_DF$Year) + c(-0.2,0.25), 
        ylim=c(0, max(test_DF$Count)*1.04), expand=FALSE) 

enter image description here

相關問題