Bart6114
2012-Mar-15 15:47 UTC
[R] Ggplot barchart drops factor levels: how to show them with zero counts?
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-them-with-zero-counts-tp4475417p4475417.html Sent from the R help mailing list archive at Nabble.com.
Sarah Goslee
2012-Mar-15 16:34 UTC
[R] Ggplot barchart drops factor levels: how to show them with zero counts?
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
Helios de Rosario
2012-Mar-16 11:43 UTC
[R] Ggplot barchart drops factor levels: how to show them with zero counts?
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 <bartsmeets86 at gmail.com>escribi?:> Hello, > > When plotting a barchart with ggplot it drops the levels of thefactor 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 forfactorlevel '4',> the label 4 dissapears from the plot. However, I would still like itto show> up, but without a bar (zero observations). > > I would like to use this for the presentation of data with aLikert-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.