绘制累积增益曲线图R

时间:2019-07-03 19:11:57

标签: r ggplot2

我正在尝试使用R中的ggplot2来生成累积增益图。基本上,我想使用ggplot2复制以下内容。

我的数据就是这个

df
# A tibble: 10 x 6
Decile  resp Cumresp  Gain Cumlift
<int>  <dbl>   <dbl> <dbl>   <dbl>
1      8301    8301  57.7    5.77
2      2449   10750  74.8    3.74
3      1337   12087  84.0    2.80
4       751   12838  89.3    2.23
5       462   13300  92.5    1.85
6       374   13674  95.1    1.58
7       252   13926  96.8    1.38
8       195   14121  98.2    1.23
9       136   14257  99.1    1.10
10       124   14381 100      1 

## Cumulative Gains Plot
ggplot(df, aes(Decile,  Gain)) +
    geom_point() +
    geom_line() +
    geom_abline(intercept =  52.3 , slope = 4.77)
    scale_y_continuous(breaks = seq(0, 100, by = 20)) +
    scale_x_continuous(breaks = c(1:10)) +
    labs(title = "Cumulative Gains Plot",
     y = "Cumulative Gain %")

但是,即使我尝试geom_abline还是我的y-axis都正确,我仍然无法获得对角线。我无法从0 to 100开始。

如果有人可以使用ggplot2来获得我的照片,我将非常感激。

预先感谢

1 个答案:

答案 0 :(得分:0)

library(dplyr); library(ggplot2)

df2 <- df %>%
  add_row(Decile = 0, Gain =0) %>%
  arrange(Decile)

ggplot(df2, aes(Decile,  Gain)) +
  geom_point() +
  geom_line() +
  # This makes another geom_line that only sees the first and last row of the data
  geom_line(data = df2 %>% slice(1, n())) +

  scale_y_continuous(breaks = seq(0, 100, by = 20), limits = c(0,100)) +
  scale_x_continuous(breaks = c(1:10)) +
  labs(title = "Cumulative Gains Plot",
       y = "Cumulative Gain %")

enter image description here