rggplot2overlapgeom-segmentposition-dodge

Is there an R function to dodge overlapping values in a geom_segment plot?


I have this data:

"","NAME","PRE","start","end","jit"
"1","A","Treatment A",2024,2024.5,0
"2","B","Treatment A",2026,2026.5,0
"3","C","Treatment A",2024,2024.5,0
"4","D","Treatment A",2026,2026.5,0
"5","E","Treatment A",2026,2026.5,0
"6","F","Treatment A",2027,2027.5,0
"7","G","Treatment A",2024,2024.5,0
"8","H","Treatment A",2027,2027.5,0
"9","I","Treatment A",2024,2024.5,0
"10","J","Treatment A",2025,2025.5,0
"11","K","Treatment A",2027,2027.5,0
"12","L","Treatment A",2024,2024.5,0
"13","M","Treatment A",2027,2027.5,0
"14","N","Treatment A",2026,2026.5,0
"15","O","Treatment A",2025,2025.5,0
"16","P","Treatment A",2024,2024.5,0
"17","Q","Treatment A",2027,2027.5,0
"18","R","Treatment A",2024,2024.5,0
"19","S","Treatment A",2025,2025.5,0
"20","T","Treatment A",2025,2025.5,0
"21","U","Treatment A",2025,2025.5,0
"22","V","Treatment A",2024,2024.5,0
"23","W","Treatment A",2026,2026.5,0
"24","X","Treatment A",2025,2025.5,0
"25","Y","Treatment A",2027,2027.5,0
"26","Z","Treatment A",2025,2025.5,0
"27","AA","Treatment A",2025,2025.5,0
"28","AB","Treatment A",2024,2024.5,0
"29","AC","Treatment A",2027,2027.5,0
"30","AD","Treatment A",2026,2026.5,0
"31","AE","Treatment A",2027,2027.5,0
"32","AF","Treatment A",2026,2026.5,0
"33","AG","Treatment A",2025,2025.5,0
"34","AH","Treatment A",2027,2027.5,0
"35","AI","Treatment A",2025,2025.5,0
"36","AJ","Treatment A",2026,2026.5,0
"37","AK","Treatment A",2026,2026.5,0
"38","A","Treatment F",2028,2028.5,0
"39","B","Treatment F",2027,2027.5,0
"40","C","Treatment F",2030,2030.5,0
"41","D","Treatment F",2032,2032.5,0
"42","E","Treatment F",2027,2027.5,0
"43","F","Treatment F",NA,NA,0
"44","G","Treatment F",2031,2031.5,0
"45","H","Treatment F",NA,NA,0
"46","I","Treatment F",2032,2032.5,0
"47","J","Treatment F",2026,2026.5,0
"48","K","Treatment F",2028,2028.5,0
"49","L","Treatment F",2031,2031.5,0
"50","M","Treatment F",NA,NA,0
"51","N","Treatment F",2027,2027.5,0
"52","O","Treatment F",2026,2026.5,0
"53","P","Treatment F",2025,2025.5,0
"54","Q","Treatment F",2028,2028.5,0
"55","R","Treatment F",2025,2025.5,0
"56","S","Treatment F",2026,2026.5,0
"57","T","Treatment F",2026,2026.5,0
"58","U","Treatment F",2026,2026.5,0
"59","V","Treatment F",2025,2025.5,0
"60","W","Treatment F",2027,2027.5,0
"61","X","Treatment F",2033,2033.5,0
"62","Y","Treatment F",2033,2033.5,0
"63","Z","Treatment F",2030,2030.5,0
"64","AA","Treatment F",2026,2026.5,0
"65","AB","Treatment F",2031,2031.5,0
"66","AC","Treatment F",2033,2033.5,0
"67","AD","Treatment F",2031,2031.5,0
"68","AE","Treatment F",2028,2028.5,0
"69","AF","Treatment F",NA,NA,0
"70","AG","Treatment F",2031,2031.5,0
"71","AH","Treatment F",2028,2028.5,0
"72","AI","Treatment F",2030,2030.5,0
"73","AJ","Treatment F",2031,2031.5,0
"74","AK","Treatment F",2027,2027.5,0
"75","A","Treatment I",2030,2030.5,0
"76","B","Treatment I",2029,2029.5,0
"77","C","Treatment I",2032,2032.5,0
"78","D","Treatment I",2034,2034.5,0
"79","E","Treatment I",2029,2029.5,0
"80","F","Treatment I",NA,NA,0
"81","G","Treatment I",2033,2033.5,0
"82","H","Treatment I",NA,NA,0
"83","I","Treatment I",2034,2034.5,0
"84","J","Treatment I",2028,2028.5,0
"85","K","Treatment I",2030,2030.5,0
"86","L","Treatment I",2033,2033.5,0
"87","M","Treatment I",NA,NA,0
"88","N","Treatment I",2029,2029.5,0
"89","O","Treatment I",2028,2028.5,0
"90","P","Treatment I",2027,2027.5,0
"91","Q","Treatment I",2030,2030.5,0
"92","R","Treatment I",2027,2027.5,0
"93","S","Treatment I",2028,2028.5,0
"94","T","Treatment I",2028,2028.5,0
"95","U","Treatment I",2028,2028.5,0
"96","V","Treatment I",2027,2027.5,0
"97","W","Treatment I",2029,2029.5,0
"98","X","Treatment I",2035,2035.5,0
"99","Y","Treatment I",2035,2035.5,0
"100","Z","Treatment I",2032,2032.5,0
"101","AA","Treatment I",2028,2028.5,0
"102","AB","Treatment I",2033,2033.5,0
"103","AC","Treatment I",2035,2035.5,0
"104","AD","Treatment I",2033,2033.5,0
"105","AE","Treatment I",2030,2030.5,0
"106","AF","Treatment I",NA,NA,0
"107","AG","Treatment I",2033,2033.5,0
"108","AH","Treatment I",2030,2030.5,0
"109","AI","Treatment I",2032,2032.5,0
"110","AJ","Treatment I",2033,2033.5,0
"111","AK","Treatment I",2029,2029.5,0
"112","A","Treatment B",2028,2028.5,0
"113","B","Treatment B",2028,2028.5,0
"114","C","Treatment B",2028,2028.5,0
"115","D","Treatment B",2028,2028.5,0
"116","E","Treatment B",2029,2029.5,0
"117","F","Treatment B",2028,2028.5,0
"118","G","Treatment B",2028,2028.5,0
"119","H","Treatment B",2029,2029.5,0
"120","I","Treatment B",2029,2029.5,0
"121","J","Treatment B",2030,2030.5,0
"122","K","Treatment B",2030,2030.5,0
"123","L","Treatment B",2029,2029.5,0
"124","M","Treatment B",2030,2030.5,0
"125","N","Treatment B",2030,2030.5,0
"126","O","Treatment B",2030,2030.5,0
"127","P","Treatment B",2030,2030.5,0
"128","Q","Treatment B",2029,2029.5,0
"129","R","Treatment B",2029,2029.5,0
"130","S","Treatment B",2031,2031.5,0
"131","T","Treatment B",2031,2031.5,0
"132","U","Treatment B",2031,2031.5,0
"133","V","Treatment B",2030,2030.5,0
"134","W","Treatment B",2031,2031.5,0
"135","X","Treatment B",2031,2031.5,0
"136","Y","Treatment B",2032,2032.5,0
"137","Z","Treatment B",2032,2032.5,0
"138","AA","Treatment B",2031,2031.5,0
"139","AB","Treatment B",2032,2032.5,0
"140","AC","Treatment B",2032,2032.5,0
"141","AD","Treatment B",2033,2033.5,0
"142","AE","Treatment B",2033,2033.5,0
"143","AF","Treatment B",2033,2033.5,0
"144","AG","Treatment B",2033,2033.5,0
"145","AH","Treatment B",2033,2033.5,0
"146","AI","Treatment B",2033,2033.5,0
"147","AJ","Treatment B",2032,2032.5,0
"148","AK","Treatment B",2032,2032.5,0
"149","A","Treatment D",NA,NA,0
"150","B","Treatment D",NA,NA,0
"151","C","Treatment D",NA,NA,0
"152","D","Treatment D",NA,NA,0
"153","E","Treatment D",NA,NA,0
"154","F","Treatment D",NA,NA,0
"155","G","Treatment D",NA,NA,0
"156","H","Treatment D",NA,NA,0
"157","I","Treatment D",NA,NA,0
"158","J","Treatment D",NA,NA,0
"159","K","Treatment D",NA,NA,0
"160","L","Treatment D",NA,NA,0
"161","M","Treatment D",NA,NA,0
"162","N","Treatment D",NA,NA,0
"163","O","Treatment D",NA,NA,0
"164","P","Treatment D",NA,NA,0
"165","Q","Treatment D",NA,NA,0
"166","R","Treatment D",NA,NA,0
"167","S","Treatment D",NA,NA,0
"168","T","Treatment D",NA,NA,0
"169","U","Treatment D",NA,NA,0
"170","V","Treatment D",NA,NA,0
"171","W","Treatment D",NA,NA,0
"172","X","Treatment D",NA,NA,0
"173","Y","Treatment D",NA,NA,0
"174","Z","Treatment D",NA,NA,0
"175","AA","Treatment D",NA,NA,0
"176","AB","Treatment D",NA,NA,0
"177","AC","Treatment D",NA,NA,0
"178","AD","Treatment D",NA,NA,0
"179","AE","Treatment D",NA,NA,0
"180","AF","Treatment D",NA,NA,0
"181","AG","Treatment D",NA,NA,0
"182","AH","Treatment D",NA,NA,0
"183","AI","Treatment D",NA,NA,0
"184","AJ","Treatment D",NA,NA,0
"185","AK","Treatment D",NA,NA,0
"186","A","Treatment E",NA,NA,0
"187","B","Treatment E",NA,NA,0
"188","C","Treatment E",NA,NA,0
"189","D","Treatment E",NA,NA,0
"190","E","Treatment E",NA,NA,0
"191","F","Treatment E",NA,NA,0
"192","G","Treatment E",NA,NA,0
"193","H","Treatment E",NA,NA,0
"194","I","Treatment E",NA,NA,0
"195","J","Treatment E",NA,NA,0
"196","K","Treatment E",NA,NA,0
"197","L","Treatment E",NA,NA,0
"198","M","Treatment E",NA,NA,0
"199","N","Treatment E",NA,NA,0
"200","O","Treatment E",NA,NA,0
"201","P","Treatment E",NA,NA,0
"202","Q","Treatment E",NA,NA,0
"203","R","Treatment E",NA,NA,0
"204","S","Treatment E",NA,NA,0
"205","T","Treatment E",NA,NA,0
"206","U","Treatment E",NA,NA,0
"207","V","Treatment E",NA,NA,0
"208","W","Treatment E",NA,NA,0
"209","X","Treatment E",NA,NA,0
"210","Y","Treatment E",NA,NA,0
"211","Z","Treatment E",NA,NA,0
"212","AA","Treatment E",NA,NA,0
"213","AB","Treatment E",NA,NA,0
"214","AC","Treatment E",NA,NA,0
"215","AD","Treatment E",NA,NA,0
"216","AE","Treatment E",NA,NA,0
"217","AF","Treatment E",NA,NA,0
"218","AG","Treatment E",NA,NA,0
"219","AH","Treatment E",NA,NA,0
"220","AI","Treatment E",NA,NA,0
"221","AJ","Treatment E",NA,NA,0
"222","AK","Treatment E",NA,NA,0
"223","A","Treatment C",2028.5,2029,0
"224","B","Treatment C",2028.5,2029,0
"225","C","Treatment C",2028.5,2029,0
"226","D","Treatment C",2028.5,2029,0
"227","E","Treatment C",2029.5,2030,0
"228","F","Treatment C",2028.5,2029,0
"229","G","Treatment C",2028.5,2029,0
"230","H","Treatment C",2029.5,2030,0
"231","I","Treatment C",2029.5,2030,0
"232","J","Treatment C",2030.5,2031,0
"233","K","Treatment C",2030.5,2031,0
"234","L","Treatment C",2029.5,2030,0
"235","M","Treatment C",2030.5,2031,0
"236","N","Treatment C",2030.5,2031,0
"237","O","Treatment C",2030.5,2031,0
"238","P","Treatment C",2030.5,2031,0
"239","Q","Treatment C",2029.5,2030,0
"240","R","Treatment C",2029.5,2030,0
"241","S","Treatment C",2031.5,2032,0
"242","T","Treatment C",2031.5,2032,0
"243","U","Treatment C",2031.5,2032,0
"244","V","Treatment C",2030.5,2031,0
"245","W","Treatment C",2031.5,2032,0
"246","X","Treatment C",2031.5,2032,0
"247","Y","Treatment C",2032.5,2033,0
"248","Z","Treatment C",2032.5,2033,0
"249","AA","Treatment C",2031.5,2032,0
"250","AB","Treatment C",2032.5,2033,0
"251","AC","Treatment C",2032.5,2033,0
"252","AD","Treatment C",2033.5,2034,0
"253","AE","Treatment C",2033.5,2034,0
"254","AF","Treatment C",2033.5,2034,0
"255","AG","Treatment C",2033.5,2034,0
"256","AH","Treatment C",2033.5,2034,0
"257","AI","Treatment C",2033.5,2034,0
"258","AJ","Treatment C",2032.5,2033,0
"259","AK","Treatment C",2032.5,2033,0
"260","A","Treatment G",2028.5,2029,0
"261","B","Treatment G",2027.5,2028,0
"262","C","Treatment G",2030.5,2031,0
"263","D","Treatment G",2032.5,2033,0
"264","E","Treatment G",2027.5,2028,0
"265","F","Treatment G",NA,NA,0
"266","G","Treatment G",2031.5,2032,0
"267","H","Treatment G",NA,NA,0
"268","I","Treatment G",2032.5,2033,0
"269","J","Treatment G",2026.5,2027,0
"270","K","Treatment G",2028.5,2029,0
"271","L","Treatment G",2031.5,2032,0
"272","M","Treatment G",NA,NA,0
"273","N","Treatment G",2027.5,2028,0
"274","O","Treatment G",2026.5,2027,0
"275","P","Treatment G",2025.5,2026,0
"276","Q","Treatment G",2028.5,2029,0
"277","R","Treatment G",2025.5,2026,0
"278","S","Treatment G",2026.5,2027,0
"279","T","Treatment G",2026.5,2027,0
"280","U","Treatment G",2026.5,2027,0
"281","V","Treatment G",2025.5,2026,0
"282","W","Treatment G",2027.5,2028,0
"283","X","Treatment G",2033.5,2034,0
"284","Y","Treatment G",2033.5,2034,0
"285","Z","Treatment G",2030.5,2031,0
"286","AA","Treatment G",2026.5,2027,0
"287","AB","Treatment G",2031.5,2032,0
"288","AC","Treatment G",2033.5,2034,0
"289","AD","Treatment G",2031.5,2032,0
"290","AE","Treatment G",2028.5,2029,0
"291","AF","Treatment G",NA,NA,0
"292","AG","Treatment G",2031.5,2032,0
"293","AH","Treatment G",2028.5,2029,0
"294","AI","Treatment G",2030.5,2031,0
"295","AJ","Treatment G",2031.5,2032,0
"296","AK","Treatment G",2027.5,2028,0
"297","A","Treatment H",2029,2029.5,0
"298","B","Treatment H",2028,2028.5,0
"299","C","Treatment H",2031,2031.5,0
"300","D","Treatment H",2033,2033.5,0
"301","E","Treatment H",2028,2028.5,0
"302","F","Treatment H",NA,NA,0
"303","G","Treatment H",2032,2032.5,0
"304","H","Treatment H",NA,NA,0
"305","I","Treatment H",2033,2033.5,0
"306","J","Treatment H",2027,2027.5,0
"307","K","Treatment H",2029,2029.5,0
"308","L","Treatment H",2032,2032.5,0
"309","M","Treatment H",NA,NA,0
"310","N","Treatment H",2028,2028.5,0
"311","O","Treatment H",2027,2027.5,0
"312","P","Treatment H",2026,2026.5,0
"313","Q","Treatment H",2029,2029.5,0
"314","R","Treatment H",2026,2026.5,0
"315","S","Treatment H",2027,2027.5,0
"316","T","Treatment H",2027,2027.5,0
"317","U","Treatment H",2027,2027.5,0
"318","V","Treatment H",2026,2026.5,0
"319","W","Treatment H",2028,2028.5,0
"320","X","Treatment H",2034,2034.5,0
"321","Y","Treatment H",2034,2034.5,0
"322","Z","Treatment H",2031,2031.5,0
"323","AA","Treatment H",2027,2027.5,0
"324","AB","Treatment H",2032,2032.5,0
"325","AC","Treatment H",2034,2034.5,0
"326","AD","Treatment H",2032,2032.5,0
"327","AE","Treatment H",2029,2029.5,0
"328","AF","Treatment H",NA,NA,0
"329","AG","Treatment H",2032,2032.5,0
"330","AH","Treatment H",2029,2029.5,0
"331","AI","Treatment H",2031,2031.5,0
"332","AJ","Treatment H",2032,2032.5,0
"333","AK","Treatment H",2028,2028.5,0
"334","A","Treatment I",2031,2031.5,0
"335","A","Treatment I",2032,2032.5,0
"336","A","Treatment I",2033,2033.5,0
"337","B","Treatment I",2030,2030.5,0
"338","B","Treatment I",2031,2031.5,0
"339","B","Treatment I",2032,2032.5,0
"340","C","Treatment I",2033,2033.5,0
"341","C","Treatment I",2034,2034.5,0
"342","C","Treatment I",2035,2035.5,0
"343","D","Treatment I",2035,2035.5,0
"344","D","Treatment I",2036,2036.5,0
"345","D","Treatment I",2037,2037.5,0
"346","E","Treatment I",2030,2030.5,0
"347","E","Treatment I",2031,2031.5,0
"348","E","Treatment I",2032,2032.5,0
"349","F","Treatment I",NA,NA,0
"350","F","Treatment I",NA,NA,0
"351","F","Treatment I",NA,NA,0
"352","G","Treatment I",2034,2034.5,0
"353","G","Treatment I",2035,2035.5,0
"354","G","Treatment I",2036,2036.5,0
"355","H","Treatment I",NA,NA,0
"356","H","Treatment I",NA,NA,0
"357","H","Treatment I",NA,NA,0
"358","I","Treatment I",2035,2035.5,0
"359","I","Treatment I",2036,2036.5,0
"360","I","Treatment I",2037,2037.5,0
"361","K","Treatment I",2031,2031.5,0
"362","K","Treatment I",2032,2032.5,0
"363","K","Treatment I",2033,2033.5,0
"364","L","Treatment I",2034,2034.5,0
"365","L","Treatment I",2035,2035.5,0
"366","L","Treatment I",2036,2036.5,0
"367","M","Treatment I",NA,NA,0
"368","M","Treatment I",NA,NA,0
"369","M","Treatment I",NA,NA,0
"370","N","Treatment I",2030,2030.5,0
"371","N","Treatment I",2031,2031.5,0
"372","N","Treatment I",2032,2032.5,0
"373","O","Treatment I",2029,2029.5,0
"374","O","Treatment I",2030,2030.5,0
"375","O","Treatment I",2031,2031.5,0
"376","P","Treatment I",2028,2028.5,0
"377","P","Treatment I",2029,2029.5,0
"378","P","Treatment I",2030,2030.5,0
"379","Q","Treatment I",2031,2031.5,0
"380","Q","Treatment I",2032,2032.5,0
"381","Q","Treatment I",2033,2033.5,0
"382","R","Treatment I",2028,2028.5,0
"383","R","Treatment I",2029,2029.5,0
"384","R","Treatment I",2030,2030.5,0
"385","V","Treatment I",2028,2028.5,0
"386","V","Treatment I",2029,2029.5,0
"387","V","Treatment I",2030,2030.5,0
"388","W","Treatment I",2030,2030.5,0
"389","W","Treatment I",2031,2031.5,0
"390","W","Treatment I",2032,2032.5,0
"391","X","Treatment I",2036,2036.5,0
"392","X","Treatment I",2037,2037.5,0
"393","X","Treatment I",2038,2038.5,0
"394","Y","Treatment I",2036,2036.5,0
"395","Y","Treatment I",2037,2037.5,0
"396","Y","Treatment I",2038,2038.5,0
"397","Z","Treatment I",2033,2033.5,0
"398","Z","Treatment I",2034,2034.5,0
"399","Z","Treatment I",2035,2035.5,0
"400","AA","Treatment I",2029,2029.5,0
"401","AA","Treatment I",2030,2030.5,0
"402","AA","Treatment I",2031,2031.5,0
"403","AB","Treatment I",2034,2034.5,0
"404","AB","Treatment I",2035,2035.5,0
"405","AB","Treatment I",2036,2036.5,0
"406","AC","Treatment I",2036,2036.5,0
"407","AC","Treatment I",2037,2037.5,0
"408","AC","Treatment I",2038,2038.5,0
"409","AD","Treatment I",2034,2034.5,0
"410","AD","Treatment I",2035,2035.5,0
"411","AD","Treatment I",2036,2036.5,0
"412","AE","Treatment I",2031,2031.5,0
"413","AE","Treatment I",2032,2032.5,0
"414","AE","Treatment I",2033,2033.5,0
"415","AF","Treatment I",NA,NA,0
"416","AF","Treatment I",NA,NA,0
"417","AF","Treatment I",NA,NA,0
"418","AG","Treatment I",2034,2034.5,0
"419","AG","Treatment I",2035,2035.5,0
"420","AG","Treatment I",2036,2036.5,0
"421","AH","Treatment I",2031,2031.5,0
"422","AH","Treatment I",2032,2032.5,0
"423","AH","Treatment I",2033,2033.5,0
"424","AI","Treatment I",2033,2033.5,0
"425","AI","Treatment I",2034,2034.5,0
"426","AI","Treatment I",2035,2035.5,0
"427","AJ","Treatment I",2034,2034.5,0
"428","AJ","Treatment I",2035,2035.5,0
"429","AJ","Treatment I",2036,2036.5,0
"430","AK","Treatment I",2030,2030.5,0
"431","AK","Treatment I",2031,2031.5,0
"432","AK","Treatment I",2032,2032.5,0

And this code:

 plot <- ggplot(df, aes(x=start, xend=end, y=NAME, 
                                         yend=NAME, color=PRE)) +
  geom_segment(size = 2, lineend = "round") +
  scale_y_discrete(limits=rev) +
  xlim(2023,2044) +
  theme(panel.background = element_rect(fill = 'white', color = 'white'),
        panel.grid.major.y = element_line(colour = "grey", 
                                        linetype = "solid", 
                                        linewidth = 0.1),
        panel.grid.major.x = element_line(colour = "grey", 
                                          linetype = "dashed", 
                                          linewidth = 0.05),
        legend.position = "right", legend.justification = "top",
        legend.text = element_text(size = 6),
        legend.title = element_text(size = 10, face = "bold"),
        axis.text=element_text(size = 6),
        axis.title = element_text(size = 10, face = "bold", margin = margin(t = 10)),
        plot.title = element_text(size = 14, face = "bold")) +
  guides(col = guide_legend(ncol = 1,
                            title.position = "top",
                            keyheight = 0.3,
                            keywidth = 0.6))

Currently yielding this result: output plot

As you can see, there are instances where a common start date between prescriptions creates an overlap where you can only see one of the prescriptions. This happens frequently.

Is there an eloquent way to utilize a dodge or jitter (or similar) in a geom_segment() plot so that in instances where there is more than one prescription on the same start, they offset, revealing both. The possibility of having three aligning prescriptions on the same start is somewhat remote, but not impossible, so ideally the solution would scale so that should that issue arise, it would address that as well.


Solution

  • The main challenge is counting the number of simultaneous prescriptions per individual and converting this into a grouping variable which you can use for dodging via position_nudge

    To do this properly, you will probably need to find all co-occurring prescriptions within each group as an adjacency matrix and get the connected components of the implied graph. This isn't trivial.

    plot_df <- df %>%
      filter(!is.na(start)) %>%
      split(.$NAME) %>%
      lapply(function(x) {
        d <- within(x, instance <- seq(nrow(x)))
      
        groups <- outer(seq(nrow(d)), seq(nrow(d)), function(i, j) {
          (d$start[i] <= d$start[j] & d$end[i] >= d$start[j]) |
            (d$start[j] <= d$start[i] & d$end[j] >= d$start[i]) 
        }) |> igraph::graph_from_adjacency_matrix() |>
          igraph::components()
        
        d$group <- groups$membership
        d %>%
          group_by(group) %>%
          mutate(pos = row_number()) %>%
          mutate(pos = (pos - mean(pos))/8)
      }) %>%
      bind_rows()
    

    The thickness of your lines and large number of individuals on the plot doesn't leave much room for dodging, so the final plot loses some of its charm and is quite confusing to look at.

    ggplot(plot_df, aes(y = NAME, xmin = start, xmax = end, colour = PRE, 
                 group = dodge_group)) +
      geom_linerange(position = position_nudge(y = plot_df$dodge_group), 
                     linewidth = 1,
                     lineend = 'round', key_glyph = draw_key_path) +
      scale_y_discrete(limits = rev) +
      xlim(2023, 2044) +
      theme_minimal() 
    

    enter image description here

    If you limit the number of entries on the y axis to say, 12 individuals, this isn't as much of a problem:

    ggplot(plot_df %>% filter(NAME %in% LETTERS[1:12]),
           aes(y = NAME, xmin = start, xmax = end, colour = PRE)) +
      geom_linerange(position = position_nudge(
        y = plot_df %>% filter(NAME %in% LETTERS[1:12]) %>% pull(pos)),
        linewidth = 2,
        lineend = 'round', key_glyph = draw_key_path) +
      scale_y_discrete(limits = rev) +
      xlim(2023, 2044) +
      theme_minimal() 
    

    enter image description here