Bar graph in ggplot2 with width as a variable and even spacing between bars

后端 未结 2 943
执笔经年
执笔经年 2021-01-02 03:41

So I am trying to make a stacked bar graph with bar width mapped to a variable; but I want the spacing between my bars to be constant.

Does anyone know how to make t

2条回答
  •  清酒与你
    2021-01-02 04:21

    Attempt # 2.

    I'm tricking ggplot2 into writing a continuous scale as categorical.

    # The numbers for tmp I calculated by hand. Not sure how to program 
    # this part but the math is 
    # last + half(previous_width) + half(current_width)
    # Change the 1st number in cumsum to adjust the between category width
    

    tmp <- c(2.91,7.02,14.715,27.75,38.22,46.47,57.845,77.19,101.235) + cumsum(rep(5,9))

    dd$x.pos1 <- rep(tmp,each=5)
    ggplot(dd,aes(x=x.pos1,y=value.y,fill=Date)) +
    geom_bar(aes(width=value.x),stat="identity",position="stack") + 
    scale_x_continuous(breaks=tmp,labels=levels(dd$variable)) + 
    coord_flip()
    

    enter image description here

    For good measure you're probably going to want to adjust the text size. That's done with ... + opts(axis.text.y=theme_text(size=12))

提交回复
热议问题