【问题标题】:how to ggplot the CDF of multiple variables in r?如何ggplot r中多个变量的CDF?
【发布时间】:2020-12-24 15:23:14
【问题描述】:

我有DF data.frame。我想使用DF 中的variablesplotcumulative distribution function (CDF) 使用ggplot。使用以下代码生成plot,但由于variables 的数据范围很大,我看不到plot。我不想使用multiple facets- 想在single panel 上拥有所有variables plotted

library(ggplot2)

set.seed(123)

DF <- melt(data.frame(p1 = runif(200,1,10), p2 = runif(200,-2,1), p3 = runif(200,0,0.05),p4 = runif(200,100,4000)))

ggplot(DF, aes(x = value, col = variable))+
  stat_ecdf(lwd = 1.2)

【问题讨论】:

  • 分面是显而易见的解决方案,但设置 alpha 可能会有所帮助

标签: r dataframe ggplot2 cdf ecdf


【解决方案1】:

如果您不想使用分面,可以使用对数刻度:

library(ggplot2)

set.seed(123)

DF <- reshape::melt(data.frame(p1 = runif(200,1,10), p2 = runif(200,-2,1), p3 = runif(200,0,0.05),p4 = runif(200,100,4000)))

ggplot(DF, aes(x = value, col = variable),log='x')+
  stat_ecdf(lwd = 1.2)+
  scale_x_log10()

【讨论】:

    【解决方案2】:

    我们可以使用facet_wrap来识别

    ggplot(DF, aes(x = value, col = variable))+
       stat_ecdf(lwd = 1.2) +
       facet_wrap(~ variable)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-12
      • 2013-02-04
      • 2020-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-26
      • 2021-12-31
      相关资源
      最近更新 更多