r - ggplot2: differentiate lines by both color and linetype -


so when have plot lot of lines can differentiate them color or linetype

library(ggplot2) pd = cbind.data.frame(x = rep(c(1,2), each = 4),                y = rep(1:4, times=2),                type = rep(c("a", "b", "c", "d"), times=2)) ggplot(pd, aes(x=x, y=y, col=type)) + geom_line() + theme_bw() ggplot(pd, aes(x=x, y=y, lty=type)) + geom_line() + theme_bw() 

giving:

enter image description here enter image description here

but want both , want colors , linetypes chosen automatically (i.e. don't want specify manually color , linetype each type in question: ggplot2 manually specifying color & linetype - duplicate legend).

here example of desired output (plus automatically generated legend):

enter image description here

ideal command

ggplot(pd, aes(x=x, y=y, style=type)) + geom_line() + theme_bw() 

but guess there need workaround.

p.s: motivation of 20+ lines can hard differentiate color or linetype alone, why i'm looking combination of both. dashed red line different solid red line , both of yet different solid blue line. , don't want specify , choose colors , linetypes myself each time feed data ggplot.

you can differentiate lines geometric figures triangles or circles. if have few data (like 100 samples) can choose type = "b" inside plot properties, if have lot of data (like 10 thousand samples) can plot geometric figure on generated line can space using points() command can specify x , y coordinates of point. otherwise figures overlap each other. example (with code):

enter image description here

#!/usr/bin/rscript  args = commandargs(trailingonly=true) datalocation = args[1] filename = args[2] abs_path = args[3]  mydata <- read.csv(datalocation, sep = ";")  #paste0 concatenates 2 strings fullfilename = paste0(abs_path,filename)  #plot first line  png(fullfilename, width = 1300, height = 600)  plot(mydata[,1], type = "l", cex = 2, cex.axis = 3.0, lty = 1 ,cex.lab = 3.0, ylim = c(0, max(mydata)), xlab = "iteração", ylab = "valor")  limit = ncol(mydata)  if(limit > 1){      (column in 2:limit){         lines(mydata[,column], lty = 1, type = "l")     } }  for(i in 1:500){     if(i %% 100 == 0){        points(i, mydata[i,1], pch = 16, cex = 1.5)       points(i, mydata[i,2], pch = 24, cex = 1.5)       points(i, mydata[i,3], pch = 0, cex = 1.5)       points(i, mydata[i,4], pch = 15, cex = 1.5)     }  }  dev.off() 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -