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

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Bart6114
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
Reply | Threaded
Open this post in threaded view
|

Re: Ggplot barchart drops factor levels: how to show them with zero counts?

Sarah Goslee
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 <[hidden email]> 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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Ggplot barchart drops factor levels: how to show them with zero counts?

Bart6114
Thanks for the fast reply.

I'm using ggplot2 (it was a typo in my code example). However, using exactly the same example as I mentioned before, it only shows the bars for cyl=6&8.

How can the behaviour be different on differing machines? I just checked for updates and I have the latest ggplot2.

Thanks,
Bart

 
Reply | Threaded
Open this post in threaded view
|

Re: Ggplot barchart drops factor levels: how to show them with zero counts?

Bart6114
To visualize my problem a little; see this screenshot http://i40.tinypic.com/fodsm0.png. So I would like factor level 4 to show up but without a bar (zero counts). Thanks
Reply | Threaded
Open this post in threaded view
|

Re: Ggplot barchart drops factor levels: how to show them with zero counts?

Michael Weylandt
I can reproduce the OP's problem with ggplot 0.9.0 but I don't know
how to solve it: perhaps you should take this to the ggplot2 mailing
list: https://groups.google.com/group/ggplot2?pli=1

Michael

On Fri, Mar 16, 2012 at 4:17 AM, Bart6114 <[hidden email]> wrote:

> To visualize my problem a little; see this screenshot
> http://i40.tinypic.com/fodsm0.png http://i40.tinypic.com/fodsm0.png .
>
> So I would like factor level 4 to show up but without a bar (zero counts).
>
> Thanks
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Ggplot-barchart-drops-factor-levels-how-to-show-them-with-zero-counts-tp4475417p4477563.html
> Sent from the R help mailing list archive at Nabble.com.
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Ggplot barchart drops factor levels: how to show them with zero counts?

Helios de Rosario
In reply to this post by Bart6114
You can tell that levels must not be dropped with scale_x_discrete():

library(ggplot2)
mtcars$cyl<-factor(mtcars$cyl)
plot1 <- ggplot(mtcars[!mtcars$cyl==4,], aes(cyl))+geom_bar()
plot1 + scale_x_discrete(drop=FALSE)

Or explicitly set the values you want in the x axis with the argument
"limits":

plot1 + scale_x_discrete(limits=levels(mtcars$cyl))


Regards,
Helios


>>> El día 15/03/2012 a las 16:47, Bart6114 <[hidden email]>
escribió:
> 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
>
> --
> View this message in context:
>
http://r.789695.n4.nabble.com/Ggplot-barchart-drops-factor-levels-how-to-show-the

> m-with-zero-counts-tp4475417p4475417.html
> Sent from the R help mailing list archive at Nabble.com.

INSTITUTO DE BIOMECÁNICA DE VALENCIA
Universidad Politécnica de Valencia • Edificio 9C
Camino de Vera s/n • 46022 VALENCIA (ESPAÑA)
Tel. +34 96 387 91 60 • Fax +34 96 387 91 69
www.ibv.org

  Antes de imprimir este e-mail piense bien si es necesario hacerlo.
En cumplimiento de la Ley Orgánica 15/1999 reguladora de la Protección
de Datos de Carácter Personal, le informamos de que el presente mensaje
contiene información confidencial, siendo para uso exclusivo del
destinatario arriba indicado. En caso de no ser usted el destinatario
del mismo le informamos que su recepción no le autoriza a su divulgación
o reproducción por cualquier medio, debiendo destruirlo de inmediato,
rogándole lo notifique al remitente.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Ggplot barchart drops factor levels: how to show them with zero counts?

bbolker
In reply to this post by Michael Weylandt
R. Michael Weylandt <michael.weylandt <at> gmail.com> writes:

>
> I can reproduce the OP's problem with ggplot 0.9.0 but I don't know
> how to solve it: perhaps you should take this to the ggplot2 mailing
> list: https://groups.google.com/group/ggplot2?pli=1
>
> Michael
>
> On Fri, Mar 16, 2012 at 4:17 AM, Bart6114 <bartsmeets86 <at> gmail.com> wrote:
> > To visualize my problem a little; see this screenshot
> > http://i40.tinypic.com/fodsm0.png http://i40.tinypic.com/fodsm0.png .
> >
> > So I would like factor level 4 to show up but without a bar (zero counts).
> >
> > Thanks
> >

  Supposedly

ggplot(subset(mtcars,cyl!=4),aes(cyl))+stat_bin(drop=FALSE)

*should* work (see ?stat_bin) -- maybe a bug?  I would second the
advice to put this on the ggplot mailing list ...

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.