connect the dodged points with lines

I hope that the points in the figure are horizontally dodged and then connected with lines

library(tidyverse)


d <- tibble::tribble(
   ~id,  ~type,  ~value,
     1,    "a",      2,
     1,    "b",      3,
     1,    "c",      4
  )

# 1) All the points are aligned on a vertical line
d %>% 
  ggplot( aes(x = id, y = value) ) +
  geom_point( )

1

# 2) dodged
d %>% 
  ggplot( aes(x = id, y = value) ) +
  geom_point(
    aes(group = type),
    position = position_dodge(width = 0.7)
  ) 

2

# 3) Connect these points
d %>% 
  ggplot( aes(x = id, y = value) ) +
  geom_point(
    aes(group = type),
    position = position_dodge(width = 0.7)
  )  +
  geom_line( 
    aes(group = id),
    position = position_dodge(width = 0.7)
  ) 

3

Here's what i expect
3

Looking forward to your assistance. Thanks

position_dodge2() seems to do the job.

library(tidyverse)
  
  
  d <- tibble::tribble(
    ~id,  ~type,  ~value,
    1,    "a",      2,
    1,    "b",      3,
    1,    "c",      4
  )

  d %>% 
    ggplot( aes(x = id, y = value) ) +
    geom_point(
      aes(group = type),
      position = position_dodge2(width = 0.7)
    )  +
    geom_line( 
      aes(group = id),
      position = position_dodge2(width = 0.7)
    )

Created on 2024-11-06 with reprex v2.1.1

1 Like

thank you very much :smiling_face_with_three_hearts:

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.