如何使用 ggplot 绘制反向(互补)ecdf?

How to plot reverse (complementary) ecdf using ggplot?

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

这是我使用的代码

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

但是我想要反转ecdf(互补ecdf)。有什么最简单的方法可以做到这一点吗?

干杯!

因为似乎没有更简单的方法来绘制逆 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()
    

来自stat_ecdf的帮助页面:

Computed variables

x

x in data

y

cumulative density corresponding x

所以这有效:

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

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

在你的情况下,如果你想坚持使用那个包,你可以添加到 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)