geom_point() 和 geom_line() 用于 ggplot2 中同一图形上的多个数据集
我正在尝试将三个数据集绘制到同一个图表上。一个数据集应在图表上显示为一组未连接的点,而其他两个数据集应显示为连接的数据点。 我可以使用下面的代码构建图表:
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")
如代码所示,有两组点用“o”类型绘制,这意味着这些点通过一条线连接,而一组点则不是由一条线连接。我试图在 ggplot2 中重新创建它。 我在 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"))
执行上述操作,我得到了三种不同颜色的三组点,但当然红色和蓝色点并未分别连接。 如果我想连接这些点,我可以将 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()
当然,这会导致连接所有点的线,以便所有红点相互连接,所有蓝点都是相互连接,所有绿点都相互连接。但是,虽然我希望连接红色和蓝色点,但我不希望连接绿色点。有办法做到这一点吗?
我可以执行以下操作(或类似操作):
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")
使用此命令,连接红点,连接蓝色点,断开绿色点。但是,我不想这样做,因为我希望能够让所有点颜色出现在图例中(并且此解决方案中没有出现图例)。
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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
诀窍是每一层都可以有自己的数据集。因此,您必须对数据进行子集化,以从提供给
geom_line
的数据中排除L1=="p1"
: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 togeom_line
:您可以将不同的数据集输入到每个几何图形中。因此,您可以将不包括 p1 的数据集传入 geom_line 图层。像这样的东西应该有效:
You can feed a different dataset into each geom. So you can pass in a dataset excluding p1 into the geom_line layer. Something like this should work: