百分位图与ggplot2 / Bars与y和yend?

时间:2012-04-25 14:37:39

标签: r plot ggplot2 percentile

我想制作一个类似this的情节,仅给出我的data.frame中的百分位数。所以我的data.frame中的一行就是这样的:

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

这就是我直接想要转化为情节的内容。 任何帮助或建议赞赏!

1 个答案:

答案 0 :(得分: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

相关问题