2012-12-19 18 views
15

chcę dodać krzywą gęstości do histogram i culumative histogram, tak -Dodaj linie gęstości do histogram i skumulowanego histogramu

enter image description here

Oto jak daleko mogę się posunąć:

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

'Gęstość' nie jest na tej samej skali co" częstotliwość ". Jestem prawie pewny, że znajdziesz przykłady w SO, jeśli będziesz szukał nieco więcej. Zrobiłeś przeszukiwanie SO przed wysłaniem, prawda? –

+0

Ile duplikatów potrzebujesz? http://stackoverflow.com/questions/5688082/ggplot2-overlay-histogram-with-density-curve http://stackoverflow.com/questions/9246040/axis-labeling-in-r-histogram-and-stance-plots -multiple-overlays-of-density-plots 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-estimate-on-the-the-histogram –

+0

@DWin Dziękuję za sugestię, widziałem je wszystkie, ale nie mogłem wymyślić, jak nakładać zarówno kumulatywna, jak i regularna krzywa gęstości ... – jon

Odpowiedz

23

Oferowane bez wyjaśnienia:

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