【问题标题】:How to plot reverse (complementary) ecdf using ggplot?如何使用ggplot绘制反向(互补)ecdf?
【发布时间】:2016-05-14 00:49:48
【问题描述】:

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

这是我使用的代码

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

但是我希望反转 ecdf(互补 ecdf)。有什么最简单的方法吗?

干杯!

【问题讨论】:

  • 设置stat_ecdf(size=1, mapping=aes(-download_speed)) 有效吗?
  • 它不能按我想要的方式工作:(。我想要的是 y 轴的值是 (1-y) 而不是 y,所以在查看绘图时我们可以得到'...% of the sample has .... or more'而不是'....% of the sample has .... or less'

标签: r reverse ecdf


【解决方案1】:

来自stat_ecdf的帮助页面:

计算变量

x

数据中的x

是的

对应x的累积密度

所以这行得通:

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

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

【讨论】:

  • 有什么方法可以传递变量而不是 1 吗?我正在尝试使用它以编程方式将 ecdf 图放在辅助 y 轴上,例如 aes(y = ..y.. * var), stat = 'ecdf')... 在输入变量时出错,但在输入实际数字时却没有。谢谢!
  • 这个答案效果很好。对于我的用例,虽然我需要使用 geom_step 而不是 geom_line
【解决方案2】:

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-15
      • 1970-01-01
      • 2014-10-01
      • 2015-05-10
      • 2011-03-13
      • 1970-01-01
      • 2022-08-21
      相关资源
      最近更新 更多