library(lubridate)
library(ggplot2)
library(scales)
df <- as.data.frame(c(421, 448, 412, 425, 558, 571, 675, 790, 848, 718, 651, 778, 464, 497, 484, 473, 466, 450, 435, 433, 492, 478, 574, 628, rep(250, 12), 388, 383, 412, 423, 362, 400, 388, 383, 383, 358, 332, 291, 488, 470, 443, 456, 477, 473, 450, 456, 441, 469, 442, 436, rep(170, 12)))
colnames(df) <- "Value"
df$Date <- rep(rep(ymd(071231) %m+% months(1:12), 3), 2)
df$Series <- rep(c(rep("Index", 12), rep("Model", 12), rep("Base", 12)), 2)
df$Series_factor <- factor(df$Series, levels = c("Index", "Model", "Base"))
df$Universe <- c(rep("Paired", 36), rep("Exogenous", 36))
head(df)
p <- ggplot(df, aes(x=Date, y=Value, group=Series_factor))
p + geom_line(aes(colour = Series_factor)) + facet_wrap(~Universe, nrow = 2) +
labs(colour="Series") + scale_color_manual(values=c("#7fc6bc","#083642","#da0027")) > head(df) Value Date Series Series_factor Universe 1 421 2008-01-31 Index Index Paired 2 448 2008-02-29 Index Index Paired 3 412 2008-03-31 Index Index Paired 4 425 2008-04-30 Index Index Paired 5 558 2008-05-31 Index Index Paired 6 571 2008-06-30 Index Index Paired
And the script I wrote should give you this plot:
I'm desperately trying to keep the series "Index" and "Model" in solid line and the last one, Base, dashed with no success. I've tried scale_linetype_manual(values = c("solid", "solid", "dashed")) for example but it didn't work.
Id anyone have the answer, I'm happy to hear it.
Thanks,
R version 3.2.0 (2015-04-16) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 7 x64 (build 7601) Service Pack 1 locale: [1] LC_COLLATE=English_United Kingdom.1252 LC_CTYPE=English_United Kingdom.1252 [3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C [5] LC_TIME=English_United Kingdom.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] scales_0.2.4 ggplot2_1.0.1 lubridate_1.3.3 loaded via a namespace (and not attached): [1] Rcpp_0.11.6 digest_0.6.8 MASS_7.3-40 grid_3.2.0 plyr_1.8.2 [6] gtable_0.1.2 magrittr_1.5 stringi_0.4-1 reshape2_1.4.1 proto_0.3-10 [11] tools_3.2.0 stringr_1.0.0 munsell_0.4.2 colorspace_1.2-6 memoise_0.2.1
Thanks very much for your help Dennis! Now it’s working.
ggplot(df, aes(x = Date, y = Value, group = Series, color = Series, linetype = Series)) +
geom_line() +
scale_color_manual(values = c("#7fc6bc","#083642","#da0027")) +
scale_linetype_manual(values = c("solid", "solid", "dashed")) +
facet_wrap(~ Universe, nrow = 2)And this what I’ve got, exactly what I wanted: