ggplot2中同一图形上的多个数据集的geom_point()和geom_line() [英] geom_point() and geom_line() for multiple datasets on same graph in ggplot2

查看:594
本文介绍了ggplot2中同一图形上的多个数据集的geom_point()和geom_line()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将三个数据集绘制到同一个图上。一个数据集应该只出现在一组未连接的点上,而另外两个应该显示为连接的数据点。
我可以使用下面的代码构建图:

  x < -  c(1,2,3 ,4)
y <-c(1.1,1.2,1.3,1.4)
y2 <-c(2.1,2.2,2.3,2.4)
x3 < - c(4, (3.1,3.2,3.3,3.2)
p1 < - data.frame(x = x,y = y)
p2 < ; - data.frame(x = x,y = y2)
p3 < - data.frame(x = x3,y = y3)

plot(x,y,type = o,col =red)
points(x3,y3,col =darkgreen,pch = 16)
points(x,y2,type =o,col =blue )

如代码所示,有两组点绘制为o ,这意味着点通过一条线连接,其中一组点不通过线连接。我正试图在ggplot2中重新创建它。
我在ggplot2中执行以下操作:

$ $ p $ lt; code> zz < - melt(list(p1 = p1,p2 = p2 ,p3 = p3),id.vars =x)
ggplot(zz,aes(x.value,color = L1))
+ geom_point()+ scale_color_manual(Dataset,
values = c(p1=darkgreen,p2=blue,p3=red))

通过以上步骤,我可以得到三组不同颜色的三组点,当然红色和蓝色点不是分别相连的。
如果我想连接点,我可以将geom_line()添加到上面的命令中,以便我具有以下内容:

  ggplot(zz,aes(x.value,color = L1))+ geom_point()+ 
scale_color_manual(Dataset,values =
c(p1=darkgreen,p2 =blue,p3=red))+ geom_line()

导致连接所有点的线,以便所有红点彼此连接,所有蓝点彼此连接,并且所有绿点彼此连接。但是,虽然我想要连接红色和蓝色点,但我不希望绿色点连接。有没有办法做到这一点?



我可以做以下事情(或类似的事情):



<$ p (p,aes(x,y))+ geom_point(color =blue)+ geom_line(color =blue)
+ geom_point(data = p3,color =red)+
geom_line(data = p3,color =red)+ geom_point(data = p1,color =darkgreen)

使用此命令,红色点连接,蓝色连接,绿色断开连接。但是,我不想这样做,因为我希望能够在图例中显示所有点颜色(并且在此解决方案中没有图例)。

解决方案

诀窍是每个图层都可以拥有自己的数据集。因此,您必须将数据子集从提供给 geom_line 的数据中排除 L1 ==p1



  ggplot(zz,aes(x,y = value,color = L1))+ 
geom_point()+
geom_line(data = zz [zz $ L1!=p1,])+
scale_color_manual(Dataset,
values = c(p1=darkgreen,p2= blue,p3=red))


I'm trying to plot three datasets onto the same graph. One dataset should appear on the graph as just a set of unconnected points, whereas the other two should appear as connected data points. I can build the graph using the following code below:

x <- c(1,2,3,4)
y <- c(1.1,1.2,1.3,1.4)
y2 <- c(2.1,2.2,2.3,2.4)
x3 <- c(4,5,6,7)
y3 <- c(3.1,3.2,3.3,3.2)
p1 <- data.frame(x=x,y=y)
p2 <- data.frame(x=x,y=y2)
p3 <- data.frame(x=x3,y=y3)

plot(x,y,type="o", col="red")
points(x3,y3,col="darkgreen",pch=16)
points(x,y2,type="o",col="blue")

As shown in the code, there are two sets of points that are plotted with type "o", meaning that the points are connected by a line, where as one set of points is not connected by a line. I was trying to recreate this in ggplot2. I do the following in ggplot2:

zz <- melt(list(p1=p1,p2=p2,p3=p3), id.vars="x")
ggplot(zz, aes(x.value, color = L1)) 
+ geom_point() + scale_color_manual("Dataset", 
values = c("p1" = "darkgreen", "p2" = "blue", "p3" = "red"))

Doing the above, I get the three sets of points in three different colors, yet of course the red and blue points are not connected respectively. If I want to connect the points I can add geom_line() to the command above so that I have the following:

ggplot(zz, aes(x.value, color = L1)) + geom_point() + 
scale_color_manual("Dataset", values = 
c("p1" = "darkgreen", "p2" = "blue", "p3" = "red")) + geom_line()

Of course this results in lines connecting all the points, so that all red points are connected to each other, all blue points are connected to each other, and all green points are connected to each other. However, while I want the red and blue points to be connected, I don't want the green points to be connected. Is there a way to do this?

I could do the following (or similar to it):

ggplot(p2, aes(x,y)) + geom_point(color = "blue") + geom_line(color="blue") 
+ geom_point(data=p3, color = "red") + 
geom_line(data=p3, color="red") + geom_point(data=p1, color = "darkgreen")

With this command, the red dots are connected, the blue are connected, and the green are disconnected. However, I do not want to do this as I want to be able to have all the point colors appear in the legend (and no legend appears in this solution).

解决方案

The trick is that each layer can have its own dataset. So you have to subset the data to exclude L1=="p1" from the data provided to geom_line:

ggplot(zz, aes(x, y=value, color=L1)) + 
  geom_point() + 
  geom_line(data=zz[zz$L1!="p1", ]) +
  scale_color_manual("Dataset", 
       values = c("p1" = "darkgreen", "p2" = "blue", "p3" = "red"))

这篇关于ggplot2中同一图形上的多个数据集的geom_point()和geom_line()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆