Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

plot lines instead of points R

Tags:

r

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?

like image 881
Francisco Avatar asked Dec 31 '12 11:12

Francisco


People also ask

How do you plot multiple lines on a graph in R?

In this method to create a ggplot with multiple lines, the user needs to first install and import the reshape2 package in the R console and call the melt() function with the required parameters to format the given data to long data form and then use the ggplot() function to plot the ggplot of the formatted data.


2 Answers

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

like image 95
agstudy Avatar answered Oct 19 '22 18:10

agstudy


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.

like image 4
Kevin Ushey Avatar answered Oct 19 '22 19:10

Kevin Ushey