2012-04-25 79 views
1

我想要像this情節只給我data.frame百分。所以在我的data.frame中的一行將是這樣的:ggplot2的百分位圖/ y和yend的酒吧?

Name; q0.05; q0.25; q0.45; q0.55; q0.75; q0.95 
Italy; 76; 88; 95; 109; 112; 123 

這就是我直接想要翻譯成情節。 任何幫助或建議表示讚賞!

回答

11

一個完整的可再現的例子(像什麼,也許你可以提供了...):

dat <- data.frame(Name = factor(sample(letters[1:5],1000,replace = TRUE)), 
        val = runif(1000)) 

datSum <- ddply(dat,.(Name),summarise,q05 = quantile(val,0.05), 
            q25 = quantile(val,0.25), 
            q45 = quantile(val,0.45), 
            q55 = quantile(val,0.55), 
            q75 = quantile(val,0.75), 
            q95 = quantile(val,0.95)) 
datSum$NameAlt <- 1:5 

和劇情:

ggplot(datSum,aes(ymin = NameAlt - 0.2,ymax = NameAlt + 0.2)) + 
    geom_rect(aes(xmin = q05,xmax = q95),fill = "white",colour = "black") + 
    geom_rect(aes(xmin = q25,xmax = q75),fill = 'lightblue',colour = "black") + 
    geom_rect(aes(xmin = q45,xmax = q55),fill = 'blue',colour = "black") + 
    scale_y_continuous(breaks = 1:5,labels = datSum$Name) 

enter image description here

+0

太謝謝你了。也爲了做出這樣的努力。 – 2012-04-25 15:33:46