将密度线添加到直方图和累积直方图 [英] Add density lines to histogram and cumulative histogram

查看:260
本文介绍了将密度线添加到直方图和累积直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将密度曲线添加到直方图和生成直方图中,像这样 -

<$ p

这里是我可以去的地方:

 hist.cum < - 函数(x,plot = TRUE,...){
h < - hist(x,plot = FALSE,...)
h $计数< - cumsum(h $计数)
h $密度< - cumsum(h $密度)
h $ itensities< - cumsum(h $计数)
if )
plot(h)
h
}
x < - rnorm(100,15,5)
hist.cum(x)
hist(x ,add = TRUE,col =lightseagreen)


lines(density(x),add = TRUE,col =red)


<$ p $


$ b

解决方案

(0:30,200,替换= T,概率= 15 - 绝对(15 - 0:30))
样品数据
x <
##计算并绘制两个直方图
hcum <-h < - hist(x,plot = FALSE)
hcum $计数< - cumsum(hcum $ counts)
plot(hcum,main =)
plot(h,add = T,col =gray)

##绘制密度和累积密度,其中密度和累积密度分别为密度(b)和密度(b)。
lines(x = d $ x,y = cumsum(d $ y)/ max(cumsum(d $ y))* length(x),lwd = 2)


I want to add density curve to histogram and culumative histogram, like this -

Here is as far I can go:

hist.cum <- function(x, plot=TRUE, ...){
  h <- hist(x, plot=FALSE, ...)
  h$counts <- cumsum(h$counts)
  h$density <- cumsum(h$density)
  h$itensities <- cumsum(h$itensities)
  if(plot)
    plot(h)
  h
}
 x <- rnorm(100, 15, 5)
hist.cum(x)
 hist(x, add=TRUE, col="lightseagreen")

 #
lines (density(x), add = TRUE, col="red")

解决方案

Offered without explanation:

## Make some sample data
x <- sample(0:30, 200, replace=T, prob=15 - abs(15 - 0:30))

## Calculate and plot the two histograms
hcum <- h <- hist(x, plot=FALSE)
hcum$counts <- cumsum(hcum$counts)
plot(hcum, main="")
plot(h, add=T, col="grey")

## Plot the density and cumulative density
d <- density(x)
lines(x = d$x, y = d$y * length(x) * diff(h$breaks)[1], lwd = 2)
lines(x = d$x, y = cumsum(d$y)/max(cumsum(d$y)) * length(x), lwd = 2)

这篇关于将密度线添加到直方图和累积直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆