Multiple line type, facets and color

417 views
Skip to first unread message

JC Rio

unread,
Jun 5, 2015, 11:47:48 AM6/5/15
to ggp...@googlegroups.com
Hi, in the script below, does anyone know how can I set the third time series named "Base" in dashed line?

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"))

This is my data structure:
> 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,



Brian

unread,
Jun 5, 2015, 12:32:49 PM6/5/15
to JC Rio, ggp...@googlegroups.com
why not try:
p +geom_line(aes(linetype=Series_factor))+facet_wrap(~Universe,nrow =2)

Or do you want to vary color and line type?

oh and:
!> ymd(071231) %m+% months(1:12)
Error in update(time, tz = tzone) : unused arguments (time, tz = tzone)

Not so reproducible for me

> sessionInfo()
R version 3.1.0 (2014-04-10)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] grid stats graphics grDevices utils datasets methods
[8] base

other attached packages:
[1] lubridate_1.3.3 scales_0.2.4 xtable_1.7-4
plyr_1.8.1
[5] reshape2_1.4 ggplot2_1.0.0 IDPmisc_1.1.17
lattice_0.20-29
[21] sp_1.0-16 mapdata_2.2-3 mapproj_1.2-2
maps_2.3-9
[25] chron_2.3-45 MASS_7.3-35

loaded via a namespace (and not attached):
[1] colorspace_1.2-4 compiler_3.1.0 digest_0.6.4 foreign_0.8-61
[5] gtable_0.1.2 labeling_0.3 memoise_0.2.1 munsell_0.4.2
[9] proto_0.3-10 Rcpp_0.11.3 stringr_0.6.2 tools_3.1.0
>

On 05.06.2015 16:53, JC Rio wrote:
> Hi, in the script below, does anyone know how can I set the third time
> series named "Base" in dashed line?
>
> |
> 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"))
> |
>
> This is my data structure:
> > 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:
>
> <https://lh3.googleusercontent.com/-TwoXrWqcwVA/VXG2wjpvtNI/AAAAAAAAAek/PkVSLGOMzgA/s1600/ggplot2.png>
>
>
>
> 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,
>
>
>
> --
> --
> You received this message because you are subscribed to the ggplot2
> mailing list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google
> Groups "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to ggplot2+u...@googlegroups.com
> <mailto:ggplot2+u...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout.

JC Rio

unread,
Jun 5, 2015, 1:00:24 PM6/5/15
to ggp...@googlegroups.com
Thanks for the answer.

Indeed I would like to get 3 different colors for the series, two solid lines and one dashed line. Maybe this is impossible with ggplot2? If someone has the answer I'm happy to hear it.

zenlines, I've uploaded thedata file I'm using if you can't build the data frame with lubridate, if the attachment does not work I've also put it on this link. It's a csv file, if you import it in R, don't forget to run df$Series_factor <- factor(df$Series, levels = c("Index", "Model", "Base")) to get the good order.

One last thing, this is the version I'm on:
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  
Rdata.csv

Dennis Murphy

unread,
Jun 5, 2015, 3:39:16 PM6/5/15
to JC Rio, ggplot2
This appears to have worked for me:

library(ggplot2)

# Data file moved into current working directory before reading
df <- read.csv("Rdata.csv", header = TRUE)

# Reorder the levels of the factor Series
df$Series <- factor(df$Series, levels = c("Index", "Model", "Base"))

ggplot(df, aes(x = date, y = Value, color = Series, linetype = Series)) +
geom_line(size = 1) +
scale_color_manual(values = c("#7fc6bc","#083642","#da0027")) +
scale_linetype_manual(values = c("solid", "solid", "dashed")) +
facet_wrap(~ Universe, nrow = 2)

In order to merge the two scales, you first need to map both the color
and linetype aesthetics to levels of Series before the scale*
functions can be fruitfully applied. It's not clear from your original
post that you had mapped linetype to Series before calling
scale_linetype_manual() - if you didn't, that would explain why "it
didn't work".

BTW, thank you for the reproducible example - it made things a lot
easier to diagnose and fix.

Dennis
> email to ggplot2+u...@googlegroups.com.

JC Rio

unread,
Jun 8, 2015, 4:14:52 AM6/8/15
to ggp...@googlegroups.com, hydr...@gmail.com

Thanks very much for your help Dennis! Now it’s working.

I’ve made small changes from your script, I had to write x = Date (with uppercase) and also had to add group = Series in order to get the aesthetic properly mapped.

Here is what I’ve run:


  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:

I’ve started using ggplot2 last Tuesday and don’t really know all the tricks yet but so far I’m impressed by the quality of the charts and its simplicity.

Cheers,
JC

Reply all
Reply to author
Forward
0 new messages