I want to specify two points on the line graph as below using ggplot. How can I do that?
Asked
Active
Viewed 326 times
1 Answers
3
library(ggplot2)
ggplot(mtcars, aes(wt, mpg)) +
geom_line() +
geom_segment(aes(x = 2, y = 0, xend = 2, yend = 25), linetype=2) +
geom_segment(aes(x = 0, y = 25, xend = 2, yend = 25), linetype=2) +
geom_point(data=data.frame(lat = c(25), long = c(2)),aes(long,lat),colour="blue",size=4) +
geom_segment(aes(x = 1, y = 0, xend = 1, yend = 20), linetype=2) +
geom_segment(aes(x = 0, y = 20, xend = 1, yend = 20), linetype=2) +
geom_point(data=data.frame(lat = c(20), long = c(1)),aes(long,lat),colour="blue",size=4)
You do with geom_segment and/or geom_point.
Look here for how to change the axis labels/ ticks.
EDIT: I edited my post for the sake of the second point.

Community
- 1
- 1

Robert Kirsten
- 474
- 2
- 5
- 12
-
3A suggestion: if there are multiple segments needed, you could consider creating a dataframe with all the relevant information, prevents repetition of `geom_segment`. In this case, you could do something like `segment_data <- data.frame(x=c(2,0,1,0),y=c(0,25,0,20),xend=c(2,2,1,1), yend=c(25,25,20,20))` and then `b + geom_segment(data=segment_data, aes(x=x,xend=xend,y=y,yend=yend))`. – Heroka Nov 11 '15 at 15:13