简体   繁体   English

如何通过 ggplot2 中的一个点画一条 45 度线

[英]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).我在对数刻度上有一个散点图,垂直和水平参考线穿过(1,1)(下面的虚线)。 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).我想向虚线参考线添加额外的 45 度参考线,以便它们完全平分由虚线参考线创建的四个“部分”(例如,我刚刚通过眼睛添加的下方较细的实线) . How could I do this with ggplot?我怎么能用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.当我最初尝试geom_abline(intercept = 0, slope = 1) + geom_abline(intercept = 2, slope = -1)时,它看起来不正确,所以我认为我需要一种不同的方式来做到这一点。 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()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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