Storylines instead of R points

This is probably a simple question, but I can not find a solution for this.

I have the following graph (I use the CI graph since I cannot fill the points with the graph ()).

leg<-c("1","2","3","4","5","6","7","8") Col.rar1<-c(rgb(1,0,0,0.7), rgb(0,0,1,0.7), rgb(0,1,1,0.7),rgb(0.6,0,0.8,0.7),rgb(1,0.8,0,0.7),rgb(0.4,0.5,0.6,0.7),rgb(0.2,0.3,0.2,0.7),rgb(1,0.3,0,0.7)) library(plotrix) plotCI(test$size,test$Mean, pch=c(21), pt.bg=Col.rar1,xlab="",ylab="", ui=test$Mean,li= test$Mean) legend(4200,400,legend=leg,pch=c(21),pt.bg=Col.rar1, bty="n", cex=1) 

enter image description here

I want to create the same effect, but with lines instead of dots (line continuation)

Any suggestion?

+4
source share
2 answers

When it comes to creating graphs, where you want the lines to be connected according to some grouping variable, you want to get away from the base-R sections and check lattice and ggplot2 . Base-R lines do not have a simple "group" concept in xy graphics.

A simple lattice example:

 library( lattice ) dat <- data.frame( x=rep(1:5, times=4), y=rnorm(20), gp=rep(1:4,each=5) ) xyplot( y ~ x, dat, group=gp, type='b' ) 

You should be able to use something like this if you have a variable in test similar to the color vector you define.

+5
source

You have 2 solutions:

  • Usage The lines() function draws lines between locations (x, y).
  • Use plot with type = "l" as a string

it's hard to show it without a reproducible example, but you can do, for example:

 Col.rar1<-c(rgb(1,0,0,0.7), rgb(0,0,1,0.7), rgb(0,1,1,0.7),rgb(0.6,0,0.8,0.7),rgb(1,0.8,0,0.7),rgb(0.4,0.5,0.6,0.7),rgb(0.2,0.3,0.2,0.7),rgb(1,0.3,0,0.7)) x <- seq(0, 5000, length.out=10) y <- matrix(sort(rnorm(10*length(Col.rar1))), ncol=length(Col.rar1)) plot(x, y[,1], ylim=range(y), ann=FALSE, axes=T,type="l", col=Col.rar1[1]) lapply(seq_along(Col.rar1),function(i){ lines(x, y[,i], col=Col.rar1[i]) points(x, y[,i]) # this is optional }) 

enter image description here

+17
source

All Articles