简体   繁体   English

R:如何在图形中绘制对角线

[英]R: How to draw a diagonal line in a graph

I have created a graph with ggplot2 and I would like to add a diagonal line from bottom-left to up-right and asign a color to this line.我用 ggplot2 创建了一个图表,我想添加一条从左下角到右上角的对角线,并为这条线指定颜色。 How can I do that?我怎样才能做到这一点?

ggplot(data, aes(x=DRTG, y=ORTG)) +
  geom_point(colour = "#000000") + 
  ggtitle("Gráfico: Ratio Defensivo / Ratio Ofensivo (hasta jornada 8)") +
  geom_text(label=rownames(data), colour = "#000000", nudge_x = 0, nudge_y = 0.75, size = 4, vjust = "inward", hjust = "inward", check_overlap = F) +
  geom_point(data=pointMedia, aes(x=mediaDRTG, y=mediaORTG, colour="red", size = 1)) + 
  geom_vline(xintercept = pointMedia[, "mediaDRTG"], colour = "green") + 
  geom_hline(yintercept = pointMedia[, "mediaORTG"], colour = "blue") +
  geom_text(data=pointMedia, aes(x=mediaDRTG, y=mediaORTG, nudge_x = 0, nudge_y = 0.75, label="Liga DIA")) +
  theme(legend.position = "none")

在此处输入图像描述

Edit I:编辑我:

I have added + geom_abline(intercept = 120, slope = 0) and I don't get anything:( What am I doing wrong?我添加了 + geom_abline(intercept = 120, slope = 0) 但我什么也没得到:(我做错了什么?

ggplot(data, aes(x=DRTG, y=ORTG)) +
  geom_point(colour = "#000000") + 
  ggtitle("Gráfico: Ratio Defensivo / Ratio Ofensivo (hasta jornada 8)") +
  geom_text(label=rownames(data), colour = "#000000", nudge_x = 0, nudge_y = 0.75, size = 4, vjust = "inward", hjust = "inward", check_overlap = F) +
  geom_point(data=pointMedia, aes(x=mediaDRTG, y=mediaORTG, colour="red", size = 1)) + 
  geom_vline(xintercept = pointMedia[, "mediaDRTG"], colour = "green") + 
  geom_hline(yintercept = pointMedia[, "mediaORTG"], colour = "blue") +
  geom_text(data=pointMedia, aes(x=mediaDRTG, y=mediaORTG, label="Liga DIA"), nudge_x = 0, nudge_y = 0.75) +
  geom_abline(intercept = 120, slope = 0) +
  theme(legend.position = "none")

Edit II:编辑二:

I have setting intercept and slope to this values and I get this pic我已经将截距和斜率设置为这个值,我得到了这张照片

ggplot(data, aes(x=DRTG, y=ORTG)) +
  geom_point(colour = "#000000") + 
  ggtitle("Gráfico: Ratio Defensivo / Ratio Ofensivo (hasta jornada 8)") +
  geom_text(label=rownames(data), colour = "#000000", nudge_x = 0, nudge_y = 0.75, size = 4, vjust = "inward", hjust = "inward", check_overlap = F) +
  geom_point(data=pointMedia, aes(x=mediaDRTG, y=mediaORTG, colour="red", size = 1)) + 
  geom_vline(xintercept = pointMedia[, "mediaDRTG"], colour = "green") + 
  geom_hline(yintercept = pointMedia[, "mediaORTG"], colour = "blue") +
  geom_text(data=pointMedia, aes(x=mediaDRTG, y=mediaORTG, label="Liga DIA"), nudge_x = 0, nudge_y = 0.75) +
  geom_abline(intercept = 11.6, slope = 1) +
  theme(legend.position = "none")

在此处输入图像描述

And what I want is that the end of the line is in the upper right corner of the graph.我想要的是线的末端在图表的右上角。

I want to draw a line from bottom left corner of the graph until upper right corner of the graph.我想从图表的左下角到图表的右上角画一条线。

Given nobody answered this query, I figured I would add one here so people can quickly reference it rather than skim through the comments, as I found this to be a useful thread when I first googled it.鉴于没有人回答这个问题,我想我会在这里添加一个,这样人们就可以快速参考它而不是浏览评论,因为当我第一次用谷歌搜索它时,我发现这是一个有用的线程。 First off, you can quickly and automatically draw a line across a ggplot function without even using the aes argument.首先,您甚至可以在不使用aes参数的情况下快速自动地在ggplot function 上画一条线。

ggplot()+
  geom_abline()

Like so:像这样:

对角线

Of course this isn't super useful on its own.当然,这本身并不是很有用。 Given there wasn't any reproducible data given, I can provide an example so people can try.鉴于没有给出任何可重复的数据,我可以提供一个示例,以便人们可以尝试。 Using the iris data, we can draw a simple line across the page if we want.使用iris数据,我们可以根据需要在页面上画一条简单的线。

ggplot(iris,
       aes(x=Petal.Length,
           y=Petal.Width))+
  geom_point()+
  geom_abline()

废话情节

Now of course that isn't super helpful either.当然,这也不是很有帮助。 One way we can make it more useful is by applying it to regression lines.我们可以使它更有用的一种方法是将其应用于回归线。 We first call the lm function to get us a slope and intercept value, then plug those into the geom_abline function, then color it blue.我们首先调用lm function 来获得斜率和截距值,然后将它们插入geom_abline function,然后将其涂成蓝色。

lm(Petal.Width ~ Petal.Length,
              data = iris) # int: -0.361, slope = .4158

ggplot(iris,
       aes(x=Petal.Length,
           y=Petal.Width))+
  geom_point()+
  geom_abline(slope = .4158,
              intercept = -0.3631,
              color="blue")

注册行

Maybe we want to establish cutoff zones in our plot (perhaps there is some theoretical reason that values should only lie within a certain range on the plot. We can create a couple of dashed red lines above and below the data points.也许我们想在我们的 plot 中建立截止区(也许有一些理论上的原因,即值应该只在 plot 上的某个范围内。我们可以在数据点的上方和下方创建几条红色虚线。

ggplot(iris,
       aes(x=Petal.Length,
           y=Petal.Width))+
  geom_point()+
  geom_abline(slope = .4158,
              intercept = -0.3631,
              color="blue")+
  geom_abline(slope = .6,
              intercept = 0.1,
              color="red",
              linetype = "dashed")+
  geom_abline(slope = .4,
              intercept = -1,
              color="red",
              linetype = "dashed")

在此处输入图像描述

Really there are an infinite number of ways to use geom_abline and its variants, as such it would be good to explore others like geom_hline and geom_vline if you have time.确实有无数种方法可以使用geom_abline及其变体,因此如果您有时间,最好探索一下geom_hlinegeom_vline等其他方法。

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

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