I have a list with 9 different vectors inside. And I want plot them (dot-line) in one figure with different colors by their names. How to do that in R language?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Using a made up example:
# example data:
dat <- list(a=1:5,b=2:7,c=3:10)
# get plotting:
plot(unlist(dat),type="n",xlim=c(1,max(sapply(dat,length))))
mapply(lines,dat,col=seq_along(dat),lty=2)
legend("topleft",names(dat),lty=2,col=seq_along(dat))
回答2:
No question would be complete without a ggplot answer.
dat <- list(a=1:5,b=2:7,c=3:10)
dat <- lapply(dat, function(x) cbind(x = seq_along(x), y = x))
list.names <- names(dat)
lns <- sapply(dat, nrow)
dat <- as.data.frame(do.call("rbind", dat))
dat$group <- rep(list.names, lns)
library(ggplot2)
ggplot(dat, aes(x = x, y = y, colour = group)) +
theme_bw() +
geom_line(linetype = "dotted")
To plot each line in a separate plot, use
ggplot(dat, aes(x = x, y = y, colour = group)) +
theme_bw() +
geom_line(linetype = "dotted") +
facet_wrap(~ group)