Home > Enterprise >  geom_line with group at x axis in R
geom_line with group at x axis in R

Time:11-15

I am trying to geom_line plot such that each ID get its own line. However, the current produces vertical lines instead of horizontal lines.

I already looked here and enter image description here

Desired output

enter image description here

CodePudding user response:

As highlighted in the comments, your data doesn't have an "X" variable. This would typically be something like time or location. Alternatively, manually add one based on the grouped row number. To add the legend, include a mappable value like colour in the aesthetics.

df = df %>% 
  pivot_longer(-c(ID)) %>% 
  group_by(ID) %>% 
  mutate(row = row_number())

df %>% 
  ggplot(aes(x = row, y = value, group = ID, colour = ID))  
  geom_line(size = 1)  
  labs(x = "ID",y = "value") 

enter image description here

  • Related