rggplot2linefiguregeom

geom_line not connecting as per y-axis


I want geom_line to connect 0 to 0-2 then 2-5 and 5-10, in the following order and not based on the X-axis data. Except for one point, others are correct. Here is my script.

ggplot(dat_combined, aes(x = mean_delta_13C, y = depth_cm)) +
  geom_errorbarh(aes(xmin = mean_delta_13C - sd_delta_13c, 
                     xmax = mean_delta_13C + sd_delta_13c, color = site),
                 height = 0.2, size = 0.5,  
                 position = position_dodge2(width = 0.3))   +
  geom_line(aes(group = interaction( Type, site)))+
  geom_point(aes(color = site, shape = Type),size = 3, 
             position = position_dodge2(width = 0.1)) +
  scale_color_manual(values = site_colors, labels = new_names) +
  labs(x = expression(delta^13*"C (‰)"), y = "Soil depth (cm)", color = "Ecosystem", shape = element_blank())+
  scale_x_reverse(position = "top") +
  scale_y_discrete(limits = rev(depths)) +  
  theme_pubr()

Here is the output.

One of the lines goes from depth 0 to 5-10, not following the order of depth.

Correction required highlighted in green line.


Solution

  • geom_line orders the line segments by the x component, which is causing your problem here. From ?geom_line:

    geom_path() connects the observations in the order in which they appear in the data. geom_line() connects them in order of the variable on the x axis. ... The group aesthetic determines which cases are connected together.

    Since we cannot rely on the x value to order the line, I suggest you reorder the data so that we can rely on the order within the frame itself.

    Untested (no data):

    library(dplyr)
    dat_combined %>%
      arrange(depth_cm) %>%
      ggplot(aes(x = mean_delta_13C, y = depth_cm)) +
      geom_errorbarh(aes(xmin = mean_delta_13C - sd_delta_13c, 
                         xmax = mean_delta_13C + sd_delta_13c, color = site),
                     height = 0.2, size = 0.5,  
                     position = position_dodge2(width = 0.3))   +
      geom_path(aes(group = interaction( Type, site)))+
      geom_point(aes(color = site, shape = Type),size = 3, 
                 position = position_dodge2(width = 0.1)) +
      scale_color_manual(values = site_colors, labels = new_names) +
      labs(x = expression(delta^13*"C (‰)"), y = "Soil depth (cm)", color = "Ecosystem", shape = element_blank())+
      scale_x_reverse(position = "top") +
      scale_y_discrete(limits = rev(depths)) +  
      theme_pubr()
    

    dplyr is absolutely not required, just a clear way to demonstrate what I think should work; base-R and data.table implementations should be trivial.

    Depending on how your y-value factors are set up, you may need arrange(desc(depth_cm)) instead.