R ggplot vertical and horizontal line intercept at center

ggplot2, r

Solution

Use `geom_segment`:

DF <- read.table(text=" week    mean
1   0   0   
2   2   0   
3   3   0   
4   4   0   
5   5   0   
6   6   0   
7   7   0   
8   8   8   
9   9   30  
10  10  68  
11  11  121 
12  12  189 
13  13  272", header=TRUE)


library(ggplot2)

p <- ggplot(DF, aes(x=week, y=mean)) +
  geom_segment(aes(xend=week, yend=0), color="blue") +
  geom_segment(aes(xend=0, yend=mean), color="blue") +
  geom_line() 

print(p)

Problem

I have a regression line, called "mean". X-axis is called "week". Now, I want to draw vertical and horizontal lines, from each point of the regression line, to x-axis and y-axis. Here is my data: ``` week mean 1 0 0 2 2 0 3 3 0 4 4 0 5 5 0 6 6 0 7 7 0 8 8 8 9 9 30 10 10 68 11 11 121 12 12 189 13 13 272 ``` Here is my code: ``` ggplot()+ geom_linerange(data=df2,x=df2$week, ymin=0, ymax=df2$mean, colour="#000000",size=0.1)+ geom_hline(data=df2, yintercept=df2[trunc(df2$week==30),"mean"],colour="#000000",size=0.1) ``` I have successfully draw the vertical line, using `geom_linerange`. However, the `geom_hline` just won't work. R just doesn't draw anything. I don't know, if `geom_hline` is the function I should use. I was trying to use `geom_vline` for the vertical line part, but it never worked, so I switched back to `geom_linerange`, and it worked perfectly. Thanks for any help!!

Original source