5

我目前使用 stat_ecdf 来绘制我的累积频率图。

这是我使用的代码

    cumu_plot <- ggplot(house_total_year, aes(download_speed, colour = ISP)) + 
                 stat_ecdf(size=1)

但是我希望 ecdf 被反转(互补 ecdf)。关于最简单的方法的任何想法?

干杯!

4

3 回答 3

19

从 stat_ecdf 的帮助页面:

计算变量

X

数据中的 x

是的

对应的累积密度 x

所以这有效:

p <- ggplot(dataframe_with_column_Z, aes(x=Z))

p + geom_line(aes(y = 1 - ..y..), stat='ecdf')

输出

于 2016-12-12T19:29:22.347 回答
0

因为似乎没有更简单的方法来绘制逆 ecdf,所以如果有人正在寻找解决方案,我会这样做:

  1. 从 ecdf 函数中提取信息并将其存储在新列中

    house_total_year_ecdf <- ddply(house_total_year, c("ISP"), mutate,
          ecdf = ecdf(download_speed)(unique(download_speed))*length(download_speed))
    
    #transforming the scale to (0,1)
    house_total_year_ecdf_2 <- ddply(house_total_year_ecdf, "ISP", mutate, 
          ecdf =scale(ecdf,center=min(ecdf),scale=diff(range(ecdf))))
    
  2. 使用 geom_step 和 y = 1-ecdf 绘制图形

    ggplot(house_total_year_ecdf_2, aes(download_speed,1-ecdf, colour = ISP)) +
    geom_step()
    

在此处输入图像描述

于 2016-05-14T02:23:14.970 回答
0

在您的情况下,如果您想坚持使用该软件包,您可以添加到 aes():

y = 1 - ..y..

那是,

cumu_plot <- ggplot(house_total_year, aes(download_speed, colour = ISP, y = 1 - ..y..)) + stat_ecdf(size=1)

就我而言,我制作了以下内容:

ecdf_gg3 <- ggplot(sim_output_A.m, aes(x=loss, color=plan, y = 1 - ..y..)) +
  stat_ecdf(show.legend=FALSE) +
  labs(
    title="Simulated Loss Output",
    x = "Loss amount",
    y = "Probability of exceeding amount")+
  scale_x_continuous(labels = dollar_format())+
  scale_y_continuous(labels = percent_format()) +
  scale_fill_viridis(discrete=TRUE)+
  scale_color_viridis(discrete=TRUE)

在此处输入图像描述

于 2020-08-17T02:11:56.340 回答