5

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. 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")

enter image description here

Edit I:

I have added + geom_abline(intercept = 120, slope = 0) and I don't get anything :( What am I doing wrong?

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

enter image description here

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.

José Carlos
  • 2,850
  • 12
  • 59
  • 95

1 Answers1

2

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.

ggplot()+
  geom_abline()

Like so:

Diagonal

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.

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

crapo plot

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

reg line

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.

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

enter image description here

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.

Shawn Hemelstrand
  • 2,676
  • 4
  • 17
  • 30