[英]Connect the red points with a line in ggplot
请帮我; 我使用 ggplot 制作了一个包含一些红色和蓝色点的 plot。 现在我想用一条线将红点相互连接起来,用另一条线将蓝点相互连接这些是我的代码
m <- as.factor(c(7,"12 PCA", 21, "24 PCA", "31 PCA", 38, 70))
## Then we plot the points
ggplot(pH, aes(x= m, y=All))+ ylim(60,100)+
scale_x_discrete(limits=c(7,"12 PCA", 21, "24 PCA", "31 PCA", 38, 70))+
geom_point(data=pH, aes(y=All), colour = 'red', size =1)+
geom_point(data=pH, aes(y=Test), colour = 'blue', size=1)
我怎样才能做到这一点? 谢谢
我认为通常最好尽可能不要使用独立的数据向量,而是将其放在一个框架中。 在这种情况下,将使用一列来指示这些点属于哪个“组”。
dat <- data.frame(m=c(m,m), All=c(94,95,96,95,94,95,96, 74,67,74,67,68,73,74), grp=c(rep("red",7), rep("blue",7)))
dat
# m All grp
# 1 7 94 red
# 2 12 PCA 95 red
# 3 21 96 red
# 4 24 PCA 95 red
# 5 31 PCA 94 red
# 6 38 95 red
# 7 70 96 red
# 8 7 74 blue
# 9 12 PCA 67 blue
# 10 21 74 blue
# 11 24 PCA 67 blue
# 12 31 PCA 68 blue
# 13 38 73 blue
# 14 70 74 blue
Plot 代码:
library(ggplot2)
ggplot(dat, aes(m, All, group=grp, color=grp)) +
geom_point() +
geom_line() +
scale_color_manual(values = c(blue = "blue", red = "red"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.