本文介绍了如何使用ggplot创建分面线图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 require(reshape2) foo< - 数据.frame(abs(cbind(rnorm(3),rnorm(3,mean = .8),rnorm(3,mean = .9),rnorm(3,mean = 1)))) qux< - data.frame(abs(cbind(rnorm(3),rnorm(3,mean = .3),rnorm(3,mean = .4),rnorm(1,mean = 2)))) bar < - data.frame(abs(cbind(rnorm(3,mean = .4),rorm(3,mean = .3),rorm(3,mean = .9),rorm(3,mean = 1)) )) colnames(foo)< -c(w,x,y,z) colnames(qux)< -c(w ,x,y,z) colnames(bar) rownames(foo)< -c(n,q,r) rownames(qux)< -c(n,q,r) rownames(bar)< -c(n,q,r) foo< - cbind(ID = rownames(foo),foo) bar < - cbind(ID = rownames(bar),qux) qux foo $ fn qux $ fn< - qux bar $ fn< - bar alldf< -rbind(foo,qux,bar) alldf .m< - melt(alldf) nt要做的是以 facet 格式创建一个 ggplot 线条曲线,因此它会创建一个如下所示的图形: 实际图形不包含向上线 - 这只是一个草图,因此线分离很清晰。 我的当前代码无效: library(ggplot2 )p geom_line(aes(color = ID),alpha = 0.4)p p 最好的方法是什么?解决方案试试这个: ggplot data = alldf.m,aes(x = variable,y = value,color = ID,group = ID))+ geom_line()+ facet_wrap(〜fn) I have a data frame created with this code:require(reshape2)foo <- data.frame( abs( cbind(rnorm(3),rnorm(3, mean=.8),rnorm(3, mean=.9),rnorm(3, mean=1))))qux <- data.frame( abs( cbind(rnorm(3),rnorm(3, mean=.3),rnorm(3, mean=.4),rnorm(1, mean=2))))bar <- data.frame( abs( cbind(rnorm(3,mean=.4),rnorm(3, mean=.3),rnorm(3, mean=.9),rnorm(3, mean=1))))colnames(foo) <- c("w","x","y","z")colnames(qux) <- c("w","x","y","z")colnames(bar) <- c("w","x","y","z")rownames(foo) <- c("n","q","r")rownames(qux) <- c("n","q","r")rownames(bar) <- c("n","q","r")foo <- cbind(ID=rownames(foo),foo)bar <- cbind(ID=rownames(bar),qux)qux <- cbind(ID=rownames(bar),qux)foo$fn <- "foo"qux$fn <- "qux"bar$fn <- "bar"alldf<-rbind(foo,qux,bar)alldf.m <- melt(alldf)What I want to do is to create a ggplot line curve in facet format, so it creates a graph like this:The actual graph does not contain upward lines - this is just a sketch so that the line separation is clear. My current code doesn't work: library(ggplot2) p <- ggplot(data=alldf.m, aes(x=variable)) + geom_line(aes(colour=ID),alpha=0.4) p <- p + facet_wrap( ~ fn) pWhat's the best way to do it? 解决方案 Try this:ggplot(data=alldf.m, aes(x=variable, y = value, colour = ID, group = ID)) + geom_line() + facet_wrap(~fn) 这篇关于如何使用ggplot创建分面线图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-18 21:55