2012-12-19 24 views
15

Tôi muốn thêm đường cong mật độ để biểu đồ và biểu đồ culumative, như thế này -Thêm dòng mật độ để biểu đồ và biểu đồ tích lũy

enter image description here

Dưới đây là như xa tôi có thể đi:

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

'mật độ' không có cùng tỷ lệ với' tần số'. Tôi khá chắc chắn bạn sẽ tìm thấy ví dụ làm việc trong SO nếu bạn chỉ tìm kiếm một chút. Bạn đã tìm kiếm SO trước khi đăng, phải không? –

+0

Bạn cần bao nhiêu bản sao? http://stackoverflow.com/questions/5688082/ggplot2-overlay-histogram-with-density-curve http://stackoverflow.com/questions/9246040/axis-labeling-in-r-histogram-and-density-plots -multiple-overlay-of-density-plot http://stackoverflow.com/questions/1497539/fitting-a-density-curve-to-a-histogram-in-r http://stackoverflow.com/questions/12945951/r-programming-how-to-plot-a-density-guess-on-top-of-the-histogram –

+0

@DWin Cảm ơn bạn đã đề xuất, tôi đã thấy tất cả chúng nhưng tôi không thể tìm ra cách phủ cả đường cong mật độ tích lũy và thường xuyên ... – jon

Trả lời

23

Được cung cấp mà không cần giải thích:

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

enter image description here