i have problem ploting time series data , multiple point forecasts.
i plot historical data , point forecasts. historical data should linked line, point forecasts on other hand arrow, since second forecasted value forecast_02
actualy revised forecast_01
.
libraries used:
library(ggplot2) library(plyr) library(dplyr) library(stringr) library(grid)
here dummy data:
set.seed(1) my_df <- structure(list(values = c(-0.626453810742332, 0.183643324222082, -0.835628612410047, 1.59528080213779, 0.329507771815361, -0.820468384118015, 0.487429052428485, 0.738324705129217, 0.575781351653492, -0.305388387156356 ), c = c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j"), time = c("2014-01-01", "2014-02-01", "2014-03-01", "2014-04-01", "2014-05-01", "2014-06-01", "2014-07-01", "2014-08-01", "2014-09-01", "2014-10-01"), type_of_value = c("historical", "historical", "historical", "historical", "historical", "historical", "historical", "historical", "forecast_01", "forecast_02"), time_and_forecast = c("2014-01-01", "2014-02-01", "2014-03-01", "2014-04-01", "2014-05-01", "2014-06-01", "2014-07-01", "2014-08-01", "forecast", "forecast")), .names = c("values", "c", "time", "type_of_value", "time_and_forecast"), class = c("tbl_df", "tbl", "data.frame"), row.names = c(na, -10l)
which looks this:
source: local data frame [10 x 5] values c time type_of_value time_and_forecast 1 -0.6264538 2014-01-01 historical 2014-01-01 2 0.1836433 b 2014-02-01 historical 2014-02-01 3 -0.8356286 c 2014-03-01 historical 2014-03-01 4 1.5952808 d 2014-04-01 historical 2014-04-01 5 0.3295078 e 2014-05-01 historical 2014-05-01 6 -0.8204684 f 2014-06-01 historical 2014-06-01 7 0.4874291 g 2014-07-01 historical 2014-07-01 8 0.7383247 h 2014-08-01 historical 2014-08-01 9 0.5757814 2014-09-01 forecast_01 forecast 10 -0.3053884 j 2014-10-01 forecast_02 forecast
with code below managed produce plot wanted. however, cannot historical data points linked line.
# code perfect chart ggplot(data = my_df, aes(x = time_and_forecast, y = values, color = type_of_value, group = time_and_forecast)) + geom_point(size = 5) + geom_line(arrow = arrow()) + theme_minimal()
could me link blue points line? thank you.
# sessioninfo() r version 3.2.0 (2015-04-16) platform: x86_64-w64-mingw32/x64 (64-bit) running under: windows 8 x64 (build 9200) locale: [1] lc_collate=slovenian_slovenia.1250 lc_ctype=slovenian_slovenia.1250 lc_monetary=slovenian_slovenia.1250 [4] lc_numeric=c lc_time=c attached base packages: [1] grid stats graphics grdevices utils datasets methods base other attached packages: [1] stringr_1.0.0 dplyr_0.4.1 plyr_1.8.3 ggplot2_1.0.1 loaded via namespace (and not attached): [1] rcpp_0.11.6 assertthat_0.1 digest_0.6.8 mass_7.3-40 r6_2.0.1 gtable_0.1.2 [7] dbi_0.3.1 magrittr_1.5 scales_0.2.4 stringi_0.4-1 lazyeval_0.1.10 reshape2_1.4.1 [13] labeling_0.3 proto_0.3-10 tools_3.2.0 munsell_0.4.2 parallel_3.2.0 colorspace_1.2-6
i think want:
ggplot(data = my_df, aes(x = time_and_forecast, y = values, color = type_of_value, group = 1)) + geom_point(size = 5) + geom_line(data=my_df[my_df$type_of_value=='historical',]) + geom_line(data=my_df[!my_df$type_of_value=='historical',], arrow=arrow()) + theme_minimal()
ggplot tries draw lines within x
categorical groups, fails because each group has 1 value. if specify should same group group = 1
, draw lines across groups. since wanted line historical
group , arrow between other 2 points, can make 2 geom_line()
calls on subsets of dataframe different arrow
parameters. don't know if there's way ggplot pick arrows automatically group (like color, linetype, etc).
Comments
Post a Comment