【问题标题】:R lines() command shows crossed lines [duplicate]R lines() 命令显示交叉线[重复]
【发布时间】:2021-05-10 03:22:10
【问题描述】:

当我将lines() 添加到hist() 时,我得到无法解释的交叉线,你能告诉我为什么吗?请

x <- 
  rgamma(100, shape = 1, rate = 2)

gamma_distr <-
  fitdistr(x, densfun="gamma")

pdf_gamma <- 
  dgamma(x,
         shape = gamma_distr$estimate[1],
         rate = gamma_distr$estimate[2])

hist(x, 100, freq = FALSE)
lines(x = x, y = pdf_gamma, col="red")

【问题讨论】:

  • 因为lines 按给定值的顺序绘制:lines(x[order(x)], pdf_gamma[order(x)], col="red")

标签: r plot line


【解决方案1】:

试试这个:

library(MASS)

x <- rgamma(100, shape = 1, rate = 2)

gamma_distr <- fitdistr(x, densfun="gamma")

hist(x, 100, freq = FALSE)
curve(dgamma(x,
             shape = gamma_distr$estimate[1],
             rate = gamma_distr$estimate[2]), col="red", add = T)

【讨论】:

    猜你喜欢
    • 2017-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-23
    • 2019-05-22
    相关资源
    最近更新 更多