rggplot2geomgeom-point

geom_path or geom_line to only take the first value to plot and not all


In my dataset below, I have a column (z) that I would like to plot by x and y. I dont want to plot all the z values though, only the first one for each line. In the first figure, you can see that there are a number of dot points that are in each vertical line. I'd like to have a line to connect just the top dot points and not across all the dot points (like the second figure). Is there a way I can do this?

structure(list(x = c(-64.4427, -64.4427, -64.4427, -64.3412, 
-64.3412, -64.3412, -64.3412, -64.0203, -64.0203, -63.6396, -63.6396, 
-63.6396, -63.6396, -63.6396, -63.6396, -63.6396, -63.6396, -63.2791, 
-63.2791, -63.2791, -62.7842, -62.7842, -62.7842, -62.7842, -62.7842, 
-62.7842, -62.7842, -62.7842, -61.8798, -61.8798, -61.8798, -61.8798
 ), y = c(2582, 2301, 1750, 2490, 2220, 2000, 1700, 3025, 2650, 
3320, 3271, 3000, 2801, 2601, 2300, 2051, 1798, 3561, 3201, 2201, 
3892, 3850, 3750, 3501, 3301, 3001, 2700, 2300, 4259, 4100, 3620, 
3121), z = c(28.3265878478115, 28.3130766365866, 28.2790754780365, 
28.3286882228112, 28.3134761067483, 28.2963621888498, 28.2736333444117, 
28.3343768309236, 28.3169194745408, 28.3395710355091, 28.3401632547452, 
28.3282659767822, 28.3216608667596, 28.3109917133086, 28.28969088234, 
28.2731475151318, 28.2556126481574, 28.3357781854992, 28.3201236913069, 
28.2589132983799, 28.3365221487856, 28.3374476656256, 28.3319243679285, 
28.3177414449621, 28.3080447684229, 28.293909196108, 28.2762807654194, 
28.2528794735116, 28.3319284513409, 28.320727357254, 28.3005401796587, 
28.2761402040812)), class = "data.frame", row.names = c(NA, -32L
))

ggplot(data = df, aes(x = x, y = y), fill = z, size = 1) + geom_line()

enter image description here

enter image description here


Solution

  • df <- structure(list(x = c(
      -64.4427, -64.4427, -64.4427, -64.3412,
      -64.3412, -64.3412, -64.3412, -64.0203, -64.0203, -63.6396, -63.6396,
      -63.6396, -63.6396, -63.6396, -63.6396, -63.6396, -63.6396, -63.2791,
      -63.2791, -63.2791, -62.7842, -62.7842, -62.7842, -62.7842, -62.7842,
      -62.7842, -62.7842, -62.7842, -61.8798, -61.8798, -61.8798, -61.8798
    ), y = c(
      2582, 2301, 1750, 2490, 2220, 2000, 1700, 3025, 2650,
      3320, 3271, 3000, 2801, 2601, 2300, 2051, 1798, 3561, 3201, 2201,
      3892, 3850, 3750, 3501, 3301, 3001, 2700, 2300, 4259, 4100, 3620,
      3121
    ), z = c(
      28.3265878478115, 28.3130766365866, 28.2790754780365,
      28.3286882228112, 28.3134761067483, 28.2963621888498, 28.2736333444117,
      28.3343768309236, 28.3169194745408, 28.3395710355091, 28.3401632547452,
      28.3282659767822, 28.3216608667596, 28.3109917133086, 28.28969088234,
      28.2731475151318, 28.2556126481574, 28.3357781854992, 28.3201236913069,
      28.2589132983799, 28.3365221487856, 28.3374476656256, 28.3319243679285,
      28.3177414449621, 28.3080447684229, 28.293909196108, 28.2762807654194,
      28.2528794735116, 28.3319284513409, 28.320727357254, 28.3005401796587,
      28.2761402040812
    )), class = "data.frame", row.names = c(NA, -32L))
    
    library(tidyverse)
    
    lines_df <- df |>
      arrange(x, y) |>
      summarise(maxy = max(y), .by = x)
    
    ggplot(data = df, aes(x = x, y = y, color = z), size = 1) +
      geom_point() +
      geom_line(aes(y = maxy, color = NULL), data = lines_df)
    

    enter image description here