How to draw lines from x- and y-axis?

Hello,

Can someone please let me know how i can add dashed lines (from both x and y-axis) cutting data point at specific point. I also attached an example:
Here is my code:

library(ggplot2)
library(ggpmisc)
#> Warning: package 'ggpmisc' was built under R version 4.0.3
#> 
#> Attaching package: 'ggpmisc'
#> The following object is masked from 'package:ggplot2':
#> 
#>     annotate
df <- read.csv("F:/Complete_Projects/cutoff_example.csv", stringsAsFactors = F)
colnames(df)[1] = "timedata"
colnames(df)[2] = "score"
ggplot(df, aes(x = timedata, y = score)) +
  theme_bw() +
  xlab("Storage time (Months)") +
  ylab("Staleness intensity on 15-point scale") +
  geom_point(color = "#04fbfb", size = 2) +
  geom_smooth(method=lm, linetype = "dashed", color="black") +
  coord_cartesian(ylim=c(1, 9)) + 
  scale_y_continuous(breaks=seq(1, 9, 1)) +
  stat_poly_eq(formula = y ~ x, 
               aes(label = paste(..eq.label..)), 
               parse = TRUE, label.y = 0.60) +
  stat_poly_eq(formula = y ~ x, 
               aes(label = paste(..rr.label..)), 
               parse = TRUE, label.y = 0.55) +
  theme(axis.title.x = element_text(face = "bold"),
        axis.title.y = element_text(face="bold"),
        axis.text.x= element_text(face = "bold"),
        axis.text.y = element_text(face = "bold"),
        panel.border = element_blank(),
        axis.line = element_line())
#> `geom_smooth()` using formula 'y ~ x'

Created on 2020-11-23 by the reprex package (v0.3.0)
Screenshot 2020-11-23 151904

Try:

1 Like

Checkout the linetype argument for dashes.

Here is a reproducible example:

library(tidyverse)

ggplot(mtcars, aes(mpg, wt)) +
  geom_point() +
  geom_smooth() + 
  annotate("segment", x = 10, xend = 20, y = 3, yend = 3, colour = "orange", linetype=2) +
  annotate("segment", x = 20, xend = 20, y = 0, yend = 3, colour = "orange", linetype=2)

image

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

@williaml Thanks, but still how about other line on y-axis. I need both.

library(ggplot2)
library(ggpmisc)
#> Warning: package 'ggpmisc' was built under R version 4.0.3
#> 
#> Attaching package: 'ggpmisc'
#> The following object is masked from 'package:ggplot2':
#> 
#>     annotate
df <- read.csv("F:/Complete_Projects/cutoff_example.csv", stringsAsFactors = F)
colnames(df)[1] = "timedata"
colnames(df)[2] = "score"
p <- ggplot(df, aes(x = timedata, y = score)) +
  theme_bw() +
  xlab("Storage time (Months)") +
  ylab("Staleness intensity on 15-point scale") +
  geom_point(color = "#04fbfb", size = 2) +
  geom_smooth(method=lm, linetype = "dashed", color="black") +
  coord_cartesian(ylim=c(1, 9)) + 
  scale_y_continuous(breaks=seq(1, 9, 1)) +
  stat_poly_eq(formula = y ~ x, 
               aes(label = paste(..eq.label..)), 
               parse = TRUE, label.y = 0.60) +
  stat_poly_eq(formula = y ~ x, 
               aes(label = paste(..rr.label..)), 
               parse = TRUE, label.y = 0.55) +
  theme(axis.title.x = element_text(face = "bold"),
        axis.title.y = element_text(face="bold"),
        axis.text.x= element_text(face = "bold"),
        axis.text.y = element_text(face = "bold"),
        panel.border = element_blank(),
        axis.line = element_line())
p + annotate("segment", x = 4, xend = 4, y = 0, yend = 7,
             colour = "blue")
#> `geom_smooth()` using formula 'y ~ x'

Created on 2020-11-23 by the reprex package (v0.3.0)