[R] Ggplot barchart drops factor levels: how to show them with zero counts?

Sarah Goslee sarah.goslee at gmail.com
Thu Mar 15 17:34:37 CET 2012


One possibility: update to ggplot2. The original ggplot isn't even on
CRAN any longer.

When I tried your example with ggplot2, the empty bar for 4 was
plotted as you'd expect.

Thanks for the small reproducible example.

Sarah

On Thu, Mar 15, 2012 at 11:47 AM, Bart6114 <bartsmeets86 at gmail.com> wrote:
> Hello,
>
> When plotting a barchart with ggplot it drops the levels of the factor for
> which no counts are available.
>
> For example:
>
> library(ggplot)
> mtcars$cyl<-factor(mtcars$cyl)
> ggplot(mtcars[!mtcars$cyl==4,], aes(cyl))+geom_bar()
> levels(mtcars[!mtcars$cyl==4,])
>
> This shows my problem. Because no counts are available for factorlevel '4',
> the label 4 dissapears from the plot. However, I would still like it to show
> up, but without a bar (zero observations).
>
> I would like to use this for the presentation of data with a Likert-like
> scale.
>
> Thanks in advance!
> Bart
>

-- 
Sarah Goslee
http://www.functionaldiversity.org



More information about the R-help mailing list