Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot multiple lines with the geom_abline() ggplot2 function [duplicate]

Tags:

r

ggplot2

I have a data frame with the intercepts and slopes for six lines. There are other questions that address this (i.e., this question), but when I follow the same approach, it doesn't seem to work.

When I try the following, a plot with only a single line is returned:

library(ggplot2)
library(tibble)

d <- tibble::tribble(
  ~ID,                 ~b0,                ~b1,
  1L,  -0.253642820580212, 0.0388815148531228,
  2L,  -0.247859980353316, 0.0462798786249876,
  3L,  -0.241628306421253, 0.0418616653609702,
  4L,  -0.476161762130615, 0.0216251842526953,
  5L,  -0.372079433686108, 0.0564612163378217,
  6L, -0.0983318344106016, 0.0759661386473856
)

ggplot(d, aes(intercept = b0, slope = b1)) +
  geom_abline() +
  xlim(0, 10) +
  ylim(0, 10)

plot with one line

How can I plot the six lines associated with the six intercepts and slopes?

like image 778
Joshua Rosenberg Avatar asked Jun 05 '26 21:06

Joshua Rosenberg


2 Answers

ggplot(d)  +
  geom_abline(aes(intercept = b0, slope = b1, color=factor(ID))) +
  xlim(0, 100) +
  ylim(0, 10)

enter image description here

like image 153
Marco Sandri Avatar answered Jun 07 '26 13:06

Marco Sandri


Here is what I would do:

library(tibble)

d <- tibble::tribble(
  ~ID,                 ~b0,                ~b1,
  1L,  -0.253642820580212, 0.0388815148531228,
  2L,  -0.247859980353316, 0.0462798786249876,
  3L,  -0.241628306421253, 0.0418616653609702,
  4L,  -0.476161762130615, 0.0216251842526953,
  5L,  -0.372079433686108, 0.0564612163378217,
  6L, -0.0983318344106016, 0.0759661386473856
)

ggplot(d) +
  geom_abline(aes(intercept = b0, slope = b1, group = "ID")) +
  xlim(0, 10) +
  ylim(0, 10)

Not sure about the limits though

like image 32
Daniel Winkler Avatar answered Jun 07 '26 12:06

Daniel Winkler