Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 将密度线添加到直方图和累积直方图_R_Graph_Plot_Base_Kernel Density - Fatal编程技术网

R 将密度线添加到直方图和累积直方图

R 将密度线添加到直方图和累积直方图,r,graph,plot,base,kernel-density,R,Graph,Plot,Base,Kernel Density,我想在直方图和累积直方图中添加密度曲线,如下所示: 以下是我能做的: 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)

我想在直方图和累积直方图中添加密度曲线,如下所示:

以下是我能做的:

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")

hist.cum提供,无需解释:

## 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)
##制作一些示例数据

x
密度
频率
的比例不同。我很肯定你会在中找到有用的例子,如果你再多搜索一点的话。你在发帖前就搜索过了,对吧?你需要多少副本@谢谢你的建议,我都看过了,但我不知道如何叠加累积密度曲线和规则密度曲线。。。