5

This is probably a simple question, but I´m not able to find the solution for this.

I have the following plot (I´m using plot CI since I´m not able to fill the points with plot()).

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 creat the same effect but with lines, instead of points (continue line)

Any suggestion?

Francisco
  • 381
  • 3
  • 4
  • 13

2 Answers2

22

You have 2 solutions :

  1. Use The lines() function draws lines between (x, y) locations.
  2. Use plot with type = "l" like line

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

agstudy
  • 119,832
  • 17
  • 199
  • 261
  • The OP mentioned filled circles; might be worth mentioning `pch=21` and the `col` and `bg` arguments to `points`. – drammock Jan 01 '13 at 09:43
  • @drammock maybe..but why? the OP asked for lines instead of points (so points here is optional) and the OP accepted a lattice answer. – agstudy Jan 01 '13 at 10:04
4

When it comes to generating plots where you want lines connected according to some grouping variable, you want to get away from base-R plots and check out lattice and ggplot2. Base-R plots don't have a simple concept of 'groups' in an xy plot.

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.

Kevin Ushey
  • 20,530
  • 5
  • 56
  • 88