1

I need to create a plot from a data frame that contains data from multiple files. Some of the files are labelled as reference and some as user. I want to have smooth lines for each file, but the coloring should be done by label. Here is some test data for testing:

test <- data.frame(
  y = c(1, 2, 2, 3, 3, 4, 4, 5),
  x = rep(c(1, 2), 4),
  label = c("ref", "ref", "ref", "ref", "user", "user", "user", "user"),
  file = as.character(c(1, 1, 2, 2, 3, 3, 4, 4))
)

My current make-shift solution is to use shape=file:

ggplot(test, aes(x=x, y=y, color=label, shape=file)) +
  geom_point(size=3) + 
  geom_line() +  
  guides(shape=FALSE)

Result But I wonder whether there is a solution that does not rely on the shape, because I ideally want them to be the same. I already looked at the following questions, but they were all not helpful for my case:

Marcello Zago
  • 629
  • 5
  • 19

1 Answers1

1

geom_line uses the group aesthetic to decide which points should be linked. So you can use:

ggplot(test, aes(x=x, y=y, color=label, group=file)) +
  geom_point(size=3) + 
  geom_line()

enter image description here

Your previous solution using shape worked because if group is absent ggplot uses the combinations of other discrete factors already specified (shape, fill, colour etc) as grouping factors.

George Savva
  • 4,152
  • 1
  • 7
  • 21