使用geom_abline()ggplot2函数

时间:2018-03-22 16:20:22

标签: r ggplot2

我有一个数据框,截面和斜率为六行。还有其他问题可以解决这个问题(即question),但是当我采用相同的方法时,它似乎无法发挥作用。

当我尝试以下操作时,将返回仅包含一行的图:

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

如何绘制与六个截距和斜率相关的六条线?

3 个答案:

答案 0 :(得分:3)

以下是我要做的事情:

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)

不确定限制但

答案 1 :(得分:2)

您只想将aes移至geom_abline。那是

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

我希望这有帮助!

答案 2 :(得分:2)

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

enter image description here