Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programatically highlight the maxima at each x value

Tags:

r

ggplot2

I have some data with a bunch of x values paired with different y values. I want to plot it and highlight the max y value for each x with a different color or line or something, as in the image.

library(tidyverse)

x <- c(1,1,1,1,1,      2,2,2,    3,3,3,3,3,     4,4,  5, 6,6,6,6,6,6)
y <- c(10,11,12,13,14, 22,20,21, 8,12,14,15,18, 9,10, 5, 22,21,20,9,7,5)
df <- data.frame(x,y)

positions <- unique(x)
maxes <- c(14,22,18,10,5,22)
specials <- data.frame(positions,maxes)

ggplot(df,aes(x,y))+geom_point()+geom_line(data=specials,aes(x=positions,y=maxes))

example of desired plot

My actual dataset is huge and it's not practical to manually hunt for these maxima. How can I achieve the desired result programmatically?

like image 694
S B Avatar asked Jun 29 '26 03:06

S B


2 Answers

library(ggplot2)
ggplot(df, aes(x, y)) +
  geom_point() + 
  stat_summary(fun = max, geom = "line")
like image 193
Roland Avatar answered Jun 30 '26 17:06

Roland


It may work for you?

library(dplyr)
df2 <- df |> group_by(x) |>
  summarise(y = max(y))

ggplot(df, aes(x, y), col = x) +
  geom_line(data = specials, aes(x = positions, y = maxes)) +
  geom_point(size = 5) +
  geom_point(data = df2,
             pch = 21, fill = "red", color = "black", size = 5) +
  theme_minimal() +
  scale_x_discrete(limits = 1:6) +
  scale_y_continuous(limits = c(0, 25)) +
  labs(x = "Variable Name X", y = "Variable Name Y")

Output:

enter image description here

like image 37
MetehanGungor Avatar answered Jun 30 '26 16:06

MetehanGungor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!