如何在ggplot中制作熵图,即堆叠和填充的x轴?

时间:2014-07-08 13:16:55

标签: r ggplot2

我试图改变这些数据

data = data.frame(group = c("a", "b", "c", "d"), 
                  proportion = c(0.101787806629625, 0.578844918169105, 0.11046225951544, 0.20890501568583), 
                  entropy = c(0.521351652432232, 0.519605602533547, 0.443798118049615, 0.495838610457753 ))

group proportion   entropy
    a  0.1017878 0.5213517
    b  0.5788449 0.5196056
    c  0.1104623 0.4437981
    d  0.2089050 0.4958386

进入熵图,看起来像这样

Entropy chart

我的尝试只让我到目前为止

 ggplot(data, aes(x=as.factor(proportion), y=entropy)) + 
      geom_bar(stat="identity", aes(width=proportion))

enter image description here

可以吗?

1 个答案:

答案 0 :(得分:3)

这样的事情

#transform data    
data<-data[order(data$entropy),]
data$cp<-cumsum(data$proportion)

#plot
ggplot(data, aes(xmin=cp-proportion, xmax=cp, ymin=0, ymax=entropy)) + 
   geom_rect(aes(fill=group)) + xlab("proportion") + ylab("entropy")

enter image description here