绘制不同图层中的线 - ggplot2 [英] Plot lines in different layers- ggplot2
问题描述
我有三个看起来与这些相似的向量,在R中我想将它们绘制在同一个图上
b <-c(0.3,2.3,9,9,5,12)
c <-c(3 ,7,9,7,6,10)
有谁知道我可以使用下列命令ggplot2软件包?
颜色< - rainbow(3)
plot(a,col = colors [1] ,type =l,ylim = c(min(m),max(m)),
xlab =time [h],main =bla)
lines(b,col = color [2])
lines(c,col = colors [3])
legend(x =bottomright,legend = c(a,b,c),
col = c(colors [1],colors [2],colors [3]),pch = 19,inset = 0.01)
我只能设法绘制单个线条或使用faceting来绘制它们 - 通过从矢量创建一个数据框。
欢呼!
是的,我们做。
您的数据在我们的数据框中e melt
将其转换为'long'格式: dat < - data.frame(a = a,b = b,c = c)
dat < - melt(dat)
接下来我们在数据框中添加一个明确的'x'变量:
$ $ p $ $ $ $ $ $ $ $ x< - rep(1:6,times = 3)
最后,使用以下代码:
ggplot(dat,aes(x = x,y = value))+
geom_line (aes(color = variable))+
scale_colour_manual(values = colors)+
labs(x =time [h],y =a,color =)+
opts(title =bla)
I have three vectors that look similar to these ones, in R and I would like to plot them on the same plot
a <- c(3, 6, 16, 17, 11, 21)
b <- c(0.3, 2.3, 9, 9, 5 ,12)
c <- c(3, 7, 9, 7, 6, 10)
Does anyone know can I write the following commands using the ggplot2 package?
colours <- rainbow(3)
plot(a, col=colours[1], type="l", ylim=c(min(m), max(m)),
xlab="time[h]", main="bla")
lines(b, col=colours[2])
lines(c, col=colours[3])
legend(x="bottomright", legend=c("a","b", "c"),
col=c(colours[1],colours[2],colours[3]), pch=19, inset=0.01)
I have only managed to plot individual lines or to plot them using faceting- by creating a data frame from the vectors.
Cheers!
Yes, we do.
First, place your data in a data frame and use melt
to transform it to a 'long' format:
dat <- data.frame(a=a,b=b,c=c)
dat <- melt(dat)
next we add an explicit 'x' variable to our data frame:
dat$x <- rep(1:6,times=3)
Finally, we can plot the graph using the following code:
ggplot(dat,aes(x=x,y=value)) +
geom_line(aes(colour=variable)) +
scale_colour_manual(values=colours) +
labs(x="time[h]",y="a",colour="") +
opts(title="bla")
这篇关于绘制不同图层中的线 - ggplot2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!