2016-09-20 26 views
4

私は、特定の自然現象の発生を年ごとにマッピングし、時間とともに増加することを示すデータセットを持っています。データは次のとおりです。R:棒グラフ内の実線バーの上に点線「予測」があります

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

しかし、2016年は、不完全なデータに基づいています(のみ2016年7月の終わりまで)。したがって、私は基本的には通常の棒グラフが好きですが、上に点在する2016年の '予測値'の完全な値は実線です。私ができる - 点線/「予測」そうでない場合は、テクスチャ、最終的なバーの上のセクションに追加する方法

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) 

しかし、私は考えている:私は基本的なグラフのggplot2を使用していますが、これは私がこれまで持っているものです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

関連する問題