简体   繁体   中英

Draw a line between points of different shapes in ggplot2

Here is some dummy data

d = data.frame(
  x = rep(1:8,2),
  y = c(1,1.2,1.5,2,2.6,2.9,3.1,3.2,0.7,1,1.2,1.9,2.4,2.62,2.95,2.95),
  color = rep(LETTERS[1:2], each=8),
  shape = c(rep("a",6), rep("b",2), rep("a", 5), rep("b",3))
)


ggplot(d, aes(x=x,y=y,color=color,shape=shape)) + geom_point() + scale_colour_manual(values=c("darkblue", "darkred")) + scale_shape_manual(values = c(19,1))

在此处输入图片说明

I would like to draw a line linking the points in the different colors. Because I would like to keep a space between the line and the point, I am using geom_pointline from the package lemon

ggplot(d, aes(x=x,y=y,color=color,shape=shape)) + geom_pointline() + scale_colour_manual(values=c("darkblue", "darkred")) + scale_shape_manual(values = c(19,1))

在此处输入图片说明

My issue is that the line does not connect points of different shapes together. How can I solve this problem?

Maybe aes(group = color) is what you want.

library(lemon)

ggplot(d, aes(x = x, y = y)) +
  geom_pointline(aes(group = color, color = color, shape = shape))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM