Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
351 views
in Technique[技术] by (71.8m points)

r - geom_point() and geom_line() for multiple datasets on same graph in ggplot2

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).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"))

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...