Is there any way to limit vline length in ggplot2

I am trying to show interception on a line graph using gline ggplot and hline, but I want the lines to stop at the intercept point on the graph. Is this possible in ggplot or is there another solution

library(ggplot2) pshare <- data.frame() for (i in 1:365) { pshare <- rbind(pshare,c(i, pbirthday(i,365,coincident=3))) } names(pshare) <- c("number","probability") x25 <- qbirthday(prob = 0.25, classes = 365, coincident = 3) #61 x50 <- qbirthday(prob = 0.50, classes = 365, coincident = 3) x75 <- qbirthday(prob = 0.75, classes = 365, coincident = 3) p <- qplot(number,probability,data=subset(pshare,probability<0.99)) p <- p + geom_vline(xintercept = c(x25,x50,x75)) p <- p + geom_hline(yintercept = c(0.25,0.5,0.75)) p 

So, for example, I would like the 0.25 / 61 lines to end when they met on the chart

TIA

+6
source share
1 answer

@Joran comment extension in response and example

geom_vline draws all the way through the plot; that is his goal. geom_segment will only be used between specific endpoints. This helps to make a data frame with relevant information for drawing strings.

 probs <- c(0.25, 0.50, 0.75) marks <- data.frame(probability = probs, number = sapply(probs, qbirthday, classes=365, coincident=3)) 

This simplifies the movement of lines only to the intersection.

 qplot(number,probability,data=subset(pshare,probability<0.99)) + geom_segment(data=marks, aes(xend=-Inf, yend=probability)) + geom_segment(data=marks, aes(xend=number, yend=-Inf)) 

enter image description here

+17
source

Source: https://habr.com/ru/post/907340/


All Articles