简体   繁体   中英

R Lattice Plot Multiple Lines with Specific Color

I have two problems that I am having trouble to solve for. Firstly when I do a multiple column matrix plot using lattice xyplot , I find that all the points are connected. How can I get separate disconnected lines?

x<-cbind(rnorm(10),rnorm(10))
xyplot(x~1:nrow(x),type="l")

Secondly, I am having trouble figuring out how to make one line thicker than the other. For example, given that I want column 1, then column 1's line will be thicker than that of column 2.

The lattice plotting paradigm,like that of ggplot2 that followed it, expects data to be in long format in dataframes:

dfrm <- data.frame( y=c(rnorm(10),rnorm(10)),
                    x=1:10, 
                    grp=rep(c("a","b"),each=10))
xyplot(y~x, group=grp, type="l", data=dfrm, col=c("red","blue"))

This might not be the most elegant solution but it gets the job done:

x<-cbind(rnorm(10),rnorm(10))
plot1<-xyplot(x[,1]~1:nrow(x),type="l",col="red",lwd=3)
plot2<-xyplot(x[,2]~1:nrow(x),type="l")

library(latticeExtra)
plot1+plot2

I assumed that you wanted V1 and V2 plotted against the number of observations. Otherwise you indeed only have one line. You can adjust the axis and labels according to taste.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM