繁体   English   中英

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

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

我在对数刻度上有一个散点图,垂直和水平参考线穿过(1,1)(下面的虚线)。 我想向虚线参考线添加额外的 45 度参考线,以便它们完全平分由虚线参考线创建的四个“部分”(例如,我刚刚通过眼睛添加的下方较细的实线) . 我怎么能用ggplot做到这一点? 抱歉,如果它很简单,但我就是想不出该怎么做。

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()

在此处输入图像描述

啊对不起,我是愚蠢的。 当我最初尝试geom_abline(intercept = 0, slope = 1) + geom_abline(intercept = 2, slope = -1)时,它看起来不正确,所以我认为我需要一种不同的方式来做到这一点。 但是当然它看起来不正确,因为两个轴的比例非常不同 - 线条的角度应该与这些比例一样低。 当轴具有相同的比例时,表明此方法是正确的。

第一次尝试

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()

在此处输入图像描述

当坐标轴比例相同时

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