获取散点图线以显示 ggplot 中点的正确顺序

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

我想要一条散点图线来显示 ggplot 中点的正确顺序。 y 变量应该随着 x 变量的下降而下降。基本上,这条线应该按照深度递减的顺序跟随点。

df <-  structure(list(X = 1:43, Phase = c("Phase1a", "Phase1a", "Phase1a", 
                                   "Phase1a", "Phase1a", "Phase1a", "Phase1a", "Phase1a", "Phase1a", 
                                   "Phase1a", "Phase1a", "Phase1a", "Phase1a", "Phase1a", "Phase1a", 
                                   "Phase1b", "Phase1b", "Phase1b", "Phase1b", "Phase1b", "Phase1b", 
                                   "Phase1b", "Phase1b", "Phase1b", "Phase1b", "Phase1b", "Phase1b", 
                                   "Phase1b", "Phase1b", "Phase2", "Phase2", "Phase2", "Phase2", 
                                   "Phase2", "Phase2", "Phase2", "Phase2", "Phase2", "Phase2", "Phase2", 
                                   "Phase2", "Phase2", "Phase2"), Depth = c(0.3, 2, 1, 3, 4, 5, 
                                                                            6, 7, 8, 9, 10, 11, 12, 13, 14, 0.3, 2, 1, 3, 4, 5, 6, 7, 8, 
                                                                            9, 10, 11, 12, 13, 0.3, 2, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
                                                                            13), Temp = c(14.12, 14.07105, 14.1025, 13.8125, 13.56, 13.47, 
                                                                                          13.3325, 13.0775, 12.9225, 12.425, 11.025, 9.955, 10.6333333333333, 
                                                                                          9.355, 9.23, 17.7183333333333, 17.84, 17.93, 17.39, 17.065, 16.92, 
                                                                                          16.795, 16.585, 14.92, 13.53, 12.515, 11.685, 11.32, 12.11, 11.75, 
                                                                                          11.984, 12.044, 11.97, 11.954, 11.948, 11.944, 11.858, 11.772, 
                                                                                          11.786, 11.774, 15.1, 15.1, 15.12), DO = c(9.8, 9.4475, 9.5425, 
                                                                                                                                     9.505, 9.76, 9.39, 9.345, 9.25, 9.615, 9.315, 9.2475, 9.43, 7.76, 
                                                                                                                                     7.34, 6.61, 8.975, 8.12, 8.085, 8.21, 8.235, 8.14, 8.235, 8.145, 
                                                                                                                                     8.035, 7.99, 7.595, 6.455, 5.78, 6.74, 10.6846153846154, 10.6, 
                                                                                                                                     10.57, 10.582, 10.486, 10.502, 10.54, 10.656, 10.7, 10.52, 10.616, 
                                                                                                                                     8.41, 8.11, 7.88), Cond = c(36.25, 36.25, 36.25, 36, 35.6666666666667, 
                                                                                                                                                                 35.725, 35.725, 35.225, 34.95, 34.675, 36.1, 32, 45.3333333333333, 
                                                                                                                                                                 32.5, 31, 42.4333333333333, 43.5, 44, 43.5, 42.5, 42, 42.5, 42, 
                                                                                                                                                                 40.5, 38.5, 38.5, 49.5, 41.5, 41, 37.9, 38.24, 38.24, 38.24, 
                                                                                                                                                                 38.24, 38.22, 38.22, 37.98, 38.16, 38.56, 38.92, 42, 42, 42), 
               SPCond = c(45.9, 45.65, 45.875, 45.4, 45.2, 45.4, 45.4, 45.4, 
                          45.425, 45.35, 49.1, 44.5, 61, 46, 44, 49.3333333333333, 
                          51, 51, 51, 50, 50, 50.5, 50.5, 50.5, 50.5, 51, 65.5, 56.5, 
                          54, 50.7692307692308, 50.9, 50.9, 50.7, 50.9, 50.9, 50.7, 
                          50.7, 50.9, 51.7, 52.1, 52, 52, 52)), class = "data.frame", row.names = c(NA, 
                                                                                                    -43L))

df$Phase <- as.factor(df$Var)
df$Temp <- df$val
df$Depth <- as.factor(df$Depth)
ggplot(data = df, aes(x=Temp, y=Depth, group =Phase)) + geom_line(colour = "black") + geom_point(aes(shape=Phase, color=Phase, fill = Phase), size=2) +
  scale_shape_manual(values=c(24, 21, 21, 22, 23))+
  scale_color_manual(values=c('black','black', 'black','black'))+
  scale_fill_manual(values= c('blue','green', 'grey', 'red')) +
  theme_bw() + theme(legend.title=element_blank(), panel.border = element_rect(colour = "black", fill=NA, size=1), legend.position= 'right', legend.background = element_blank()  ) + geom_vline(xintercept=4, linetype="dotted") +
  labs(x = "Tempurature (°C)", y = "Depth (m)") +                               
  scale_x_continuous(position = "top")+
  scale_y_discrete(limits=rev)

电流输出

所需输出

r ggplot2
1个回答
0
投票

尝试使用ggplot2中的scale_y_reverse()函数而不是scale_y_discrete(limits=rev)

示例:https://ggplot2.tidyverse.org/reference/scale_continuous.html#:~:text=p1%20%2B%20scale_y_reverse()

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