简体   繁体   中英

How to draw a 45-degree line through a point in ggplot2

I have a scatterplot on a log-log scale with vertical and horizontal reference lines passing through (1,1) (dashed lines below). I'd like to add additional reference lines going at 45 degrees to the dashed reference lines, so that they exactly bisect the four "sections" created by the dashed reference lines (eg, the thinner solid lines below that I just added by eye). How could I do this with ggplot? Apologies if it's simple, but I just can't think how to do it.

example_data <- data.frame(x = c(0, 5),
                           y = c(0, 50)
                           )

ggplot(example_data, aes(x = x, y = y)) +
  geom_vline(xintercept = 1, linetype = "dashed", size = 0.75) +
  geom_hline(yintercept = 1, linetype = "dashed", size = 0.75) +
  geom_abline(intercept = -10, slope = 11, size = 0.5) +  # Just eyeballed for illustration.
  geom_abline(intercept = 10, slope = -9, size = 0.5) +   # Just eyeballed for illustration.
  geom_point()

在此处输入图像描述

Ah sorry, I was being stupid. When I originally tried geom_abline(intercept = 0, slope = 1) + geom_abline(intercept = 2, slope = -1) it didn't look right so I thought there was a different way I needed to do it. But of course it didn't look right because the scales of the two axes are so different - the angles of the lines should be that low with those scales. When the axes have the same scales it shows that this method is correct.

First attempt :

example_data <- data.frame(x = c(0, 5),
                           y = c(0, 50)
                           )

ggplot(example_data, aes(x = x, y = y)) +
  geom_vline(xintercept = 1, linetype = "dashed", size = 0.75) +
  geom_hline(yintercept = 1, linetype = "dashed", size = 0.75) +
  geom_abline(intercept = 0, slope = 1, size = 0.5) +
  geom_abline(intercept = 2, slope = -1, size = 0.5) +
  geom_point()

在此处输入图像描述

When axes scales are the same :

example_data <- data.frame(x = c(0, 50),
                           y = c(0, 50)
                           )

ggplot(example_data, aes(x = x, y = y)) +
  geom_vline(xintercept = 1, linetype = "dashed", size = 0.75) +
  geom_hline(yintercept = 1, linetype = "dashed", size = 0.75) +
  geom_abline(intercept = 0, slope = 1, size = 0.5) +
  geom_abline(intercept = 2, slope = -1, size = 0.5) +
  geom_point()

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM