0

Is there a way to produce a single point at the end of a geom_line call in ggplot? I am thinking of the equivalent to the following in base R, where one finds the max of a vector, and then plots the point only there, at the end of a line as below.

Thanks for any help on this!

lines(sort(times), 1:length(times), col=cols[i], lwd=1)
points(max(times), length(times), col=cols[i], pch=16)
neilfws
  • 32,751
  • 5
  • 50
  • 63
rratnaya
  • 11
  • 2

1 Answers1

0

Single point at end:

library(ggplot2)

df <- data.frame(x = sort(times), y = 1:length(times))

p <- ggplot(df, aes(x, y)) +
  geom_line(col = cols[i], lwd = 1)

p <- p +
  geom_point(data = subset(df, x == max(x)), col = cols[i], pch = 16)

# Display the plot
print(p)
Andrew Arrow
  • 4,248
  • 9
  • 53
  • 80
  • 1
    Thank you Andrew! This is indeed useful, but I am now finding that the multiple lines are of different lengths (times) so I will find a way to calculate max(times) for each line. – rratnaya Jul 14 '23 at 14:55