0

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.

J.M
  • 25
  • 4

1 Answers1

0

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.

r2evans
  • 141,215
  • 6
  • 77
  • 149
  • I have previously factored depth. depth_cm = factor(depth_cm, levels = c("0", "0-2", "2-5", "5-10"). I tried as per your suggestions. It did not change. – J.M May 03 '23 at 12:52
  • I understand. Please edit your question and provide sample data, there is nothing I can do without `dat_combined`. See https://stackoverflow.com/q/5963269 , [mcve], and https://stackoverflow.com/tags/r/info, please use `dput`. – r2evans May 03 '23 at 12:54
  • Oops, shift from `geom_line` to `geom_path`, I forgot that in the first edit. Reload, see the change from `geom_line`, and let me know if it works. If not, sample data please :-) – r2evans May 03 '23 at 13:00
  • 1
    geom_path works great. I have updated my data as well. Now the new problem: position = position_dodge2, error bars and geom_point are a bit far away. Points moved away from error bars. – J.M May 03 '23 at 13:12