geom_line 未按 y 轴连接

问题描述 投票:0回答:1

我希望 geom_line 按以下顺序连接 0 到 0-2,然后连接 2-5 和 5-10,而不是基于 X 轴数据。除了一点,其他都是对的。这是我的数据和脚本。

structure(list(site = structure(c(2L, 2L, 2L, 2L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), levels = c("Nahulbuta", 
"La_Campana"), class = "factor"), depth_cm = structure(c(1L, 
2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), levels = c("0", 
"0-2", "2-5", "5-10"), class = "factor"), mean_delta_13C = c(-28.21, -27.15, -26.70, -26.36, -26.74, -26.42, -25.86, -25.33, -29.06, -28.23, -27.751, -27.355, -27.22, -27.76, -27.36, -26.87), sd_delta_13c = c(1.52, 0.23, 0.41, 0.16, 1.53, 0.12, 0.13, 0.25, 0.93, 0.67, 0.54, 0.75, 1.01, 0.85, 0.86,0.80),  Type = c("Reference", "Reference", "Reference", "Reference", "Reference", "Reference", "Reference", "Reference", "Post-fire", "Post-fire", "Post-fire", "Post-fire", "Post-fire", "Post-fire", "Post-fire","Post-fire")), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA, -16L), groups = structure(list(site = structure(1:2, levels = c("Nahulbuta", 
"La_Campana"), class = "factor"), .rows = structure(list(5:12, 
    c(1L, 2L, 3L, 4L, 13L, 14L, 15L, 16L)), ptype = integer(0), class = c("vctrs_list_of", 
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -2L), .drop = TRUE))   
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()

这是输出。

其中一行从深度0到5-10,不按照深度顺序。

需要更正以绿线突出显示。

r ggplot2 line figure geom
1个回答
0
投票

geom_line
x
组件对线段进行排序,这会在此处引起您的问题。来自
?geom_line

geom_path() 按照它们在数据中出现的顺序连接观察。 geom_line() 按照 x 轴上变量的顺序连接它们。 ......群体审美决定了哪些案件被联系在一起。

由于我们不能依赖

x
值来对线进行排序,我建议您重新排序数据,以便我们可以依赖框架本身的顺序。

未经测试(无数据):

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
绝对不是必需的,只是一种清晰的方式来展示我认为应该工作的东西; base-R 和
data.table
实现应该是微不足道的。

根据您的

y
值因素的设置方式,您可能需要
arrange(desc(depth_cm))
代替。

© www.soinside.com 2019 - 2024. All rights reserved.