Modifying legends in Line graph

Hi,
I have a data for which I have to get the line graph. Basically it's the data of performance of students from first, second and third exams. I need to track the students who get less that 85% and who deteriorate from first to second exams. But when I get the graph, the legends show the color of all the students as given below. I want the color enumerators who are shown in the graph.
How can I do this?

library(tidyverse)
data<-tibble::tribble(
       ~id, ~obs_num, ~score,
  "BEN001",       1L,    84L,
  "BEN001",       2L,    85L,
  "BEN002",       1L,    86L,
  "BEN002",       2L,    86L,
  "BEN002",       3L,    84L,
  "BEN003",       1L,    88L,
  "BEN003",       2L,    90L,
  "BEN004",       1L,    80L,
  "BEN004",       2L,    80L,
  "BEN005",       1L,    83L,
  "BEN005",       2L,    88L,
  "BEN006",       1L,    79L,
  "BEN006",       2L,    80L
  )

data %>% 
  filter(score<85) %>% 
  ggplot(aes(obs_num,score,group=id,color=as.factor(id)))+
  geom_line()


Created on 2022-08-17 by the reprex package (v2.0.1)

I think its only BEN003 that wouldnt get drawn, and you are wanting to omit the legend for it ?
if so do this:


data %>% 
  filter(score<85) %>% mutate(id=forcats::fct_drop(id)) %>%
  ggplot(aes(obs_num,score,group=id,color=id))+
  geom_line()

Oh thanks. I have added geom_point also which makes it clear.

Regards,
kuttan

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.