【问题标题】:Adding multiple points to a ggplot ecdf plot向 ggplot ecdf 图添加多个点
【发布时间】:2017-12-22 03:17:34
【问题描述】:

我正在尝试生成仅 ggplot 的 C.D.F.绘制我的一些数据。我还希望能够在顶部绘制任意数量的百分位数。我有一个解决方案可以在我的曲线上添加一个点,但对于多个值会失败。

这适用于绘制一个百分位值

TestDf <- as.data.frame(rnorm(1000))
names(TestDf) <- c("Values")
percentiles <- c(0.5)

ggplot(data = TestDf, aes(x = Values)) +
  stat_ecdf() +
  geom_point(aes(x = quantile(TestDf$Values, percentiles),
                 y = percentiles))

然而这失败了

TestDf <- as.data.frame(rnorm(1000))
names(TestDf) <- c("Values")
percentiles <- c(0.25,0.5,0.75)

ggplot(data = TestDf, aes(x = Values)) +
  stat_ecdf() +
  geom_point(aes(x = quantile(TestDf$Values, percentiles),
                 y = percentiles))

有错误

错误:美学长度必须为1或与数据相同(1000):x,y

如何向stat_ecdf() 绘图添加任意数量的点?

【问题讨论】:

  • ggplot(data = TestDf, aes(x = Values)) + stat_ecdf() + geom_point(data = data.frame(x=quantile(TestDf$Values, percentiles), y=percentiles), aes(x=x, y=y))
  • 嗨@Masoud,这正是我想要的,非常感谢!

标签: r dataframe plot ggplot2 statistics


【解决方案1】:

您需要在美学之外定义一个新数据集。 aes 指的是您用于制作 CDF 的原始数据框(在原始 ggplot 参数中)。

ggplot(data = TestDf, aes(x = Values)) +
  stat_ecdf() +
  geom_point(data = data.frame(x=quantile(TestDf$Values, percentiles),
              y=percentiles), aes(x=x, y=y))

【讨论】:

    猜你喜欢
    • 2018-08-14
    • 1970-01-01
    • 2016-03-04
    • 1970-01-01
    • 2019-11-21
    • 1970-01-01
    • 1970-01-01
    • 2016-01-14
    • 1970-01-01
    相关资源
    最近更新 更多