Change the line style depending on the third variable - R

Following my data framework using dput ():

structure(list(year = 1984:2007, ger.90.10.ratio = c(3.1, 3.09, 2.98, 2.93, 2.98, 2.96, 2.95, 3.12, 3.16, 3.13, 3.24, 3.3, 3.28, 3.24, 3.24, 3.17, 3.23, 3.31, 3.63, 3.69, 3.75, 3.91, 4.1, 3.99 ), kanzler = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L), wahljahr = c(0L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L), us.90.10.ratio = c(9.55, 9.69, 10.09, 10.23, 10.21, 9.99, 10.12, 10.22, 10.34, 10.64, 10.57, 10.11, 10.33, 10.6, 10.44, 10.42, 10.58, 10.63, 10.75, 11.22, 11.08, 11.17, 11.08, 11.18), us.80.20.ratio = c(4.36, 4.38, 4.49, 4.48, 4.45, 4.44, 4.42, 4.51, 4.6, 4.65, 4.68, 4.52, 4.61, 4.64, 4.65, 4.62, 4.56, 4.65, 4.69, 4.83, 4.76, 4.78, 4.84, 4.93), ger.ratio.diff = c(NA, -0.0100000000000002, -0.11, -0.0499999999999998, 0.0499999999999998, -0.02, -0.00999999999999979, 0.17, 0.04, -0.0300000000000002, 0.11, 0.0599999999999996, -0.02, -0.0399999999999996, 0, -0.0700000000000003, 0.0600000000000001, 0.0800000000000001, 0.32, 0.0600000000000001, 0.0600000000000001, 0.16, 0.19, -0.109999999999999), ger.ratio.change = c(0, -0.0100000000000002, -0.11, -0.0499999999999998, 0.0499999999999998, -0.02, -0.00999999999999979, 0.17, 0.04, -0.0300000000000002, 0.11, 0.0599999999999996, -0.02, -0.0399999999999996, 0, -0.0700000000000003, 0.0600000000000001, 0.0800000000000001, 0.32, 0.0600000000000001, 0.0600000000000001, 0.16, 0.19, -0.109999999999999)), row.names = c(NA, -24L), .Names = c("year", "ger.90.10.ratio", "kanzler", "wahljahr", "us.90.10.ratio", "us.80.20.ratio", "ger.ratio.diff", "ger.ratio.change" ), class = "data.frame") 

What I want to do is create a line graph that changes the line style depending on the value of the third variable. It should look like this Plot with different line style depending on presidents party affiliation

In a word: In the above data frame, I would like to build ger.90.10.ratio against the year and change the line style depending on the values ​​in the chancellor's column.

I searched everywhere (maybe my google-fu is bad?), But I really would like to know if this is possible in R.

Thank you for your help, very grateful!

+4
source share
2 answers

R basic graphics do not allow you to change the properties of the line (type / dash, color, etc.) in the middle of the line, so you need to separate the data and use lines() on parts or segments() .

This is not a complete solution, but the beginning:

Set graphical parameters (unnecessary, but I like them):

 par(las=1,bty="l") 

Create a variable for splitting (we want each segment to be differentiated):

 dat <- transform(dat,k2=c(0,cumsum(diff(kanzler)!=0))) 

Share the data:

 s <- split(dat,dat$k2) 

Set up an empty plot:

 plot(ger.90.10.ratio~year,data=dat,type="n") 

Add lines for each part:

 invisible(lapply(s, function(x) with(x,lines(year,ger.90.10.ratio,col=kanzler)))) 

enter image description here

You can make this a bit more compact (as usual) in ggplot2 , but the k2 variable we set is still useful:

 library("ggplot2") qplot(year,ger.90.10.ratio,data=dat,colour=factor(kanzler), group=k2,geom="line") 

enter image description here

+4
source

This is a solution using the lattice package to show how similar it is to basic graphics. I mainly use @Ben's idea of ​​sharing data. I use the lattice panel to give more compact .

enter image description here

 library(lattice) dat <- transform(dat,k2=c(0,cumsum(diff(kanzler)!=0))) s <- split(dat,dat$k2) xyplot(ger.90.10.ratio~year,data=dat,type=c('p'),groups=kanzler, main = 'THE PARTISAL POLITICAL ECONOMY', auto.key=list(columns=2,cex=2,text=c('Democrats','Republicans')), panel=function(x,y,...) { panel.xyplot(x,y,...) lapply(s,function(x) with(x,panel.lines(year,ger.90.10.ratio,col=kanzler))) panel.grid() }) 
+1
source

All Articles