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

后端 未结 2 958
执笔经年
执笔经年 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 03:58

    For a categorical or "discrete" scale - you can adjust the width, but it needs to be between 0 and 1. Your value.x's put it over 1, hence the overlap. You can use rescale, from the scales packages to adjust this quickly so that the within category width of the bar is representative of some other variable (in this case value.x)

    install.packages("scales")
    library(scales) 
    ggplot(dd,aes(x=variable,y=value.y,fill=Date)) +
    geom_bar(aes(width=rescale(value.x,c(0.5,1))),stat="identity",position="stack")' +
    coord_flip()
    

    Play with rescaling for optimal "view" change 0.5 to 0.25... etc.

    enter image description here

    Personally, I think something like this is more informative:

    ggplot(dd,aes(x=variable,y=value.y,fill=Date)) +
    geom_bar(aes(width=rescale(value.x,c(0.2,1))),stat="identity") +
    coord_flip() + facet_grid(~Date) + opts(legend.position="none")
    

    enter image description here

提交回复
热议问题