position dodge - side by side bars with geom_bar

430 views
Skip to first unread message

Omar André Gonzáles Díaz

unread,
Jan 17, 2016, 2:00:24 PM1/17/16
to ggplot2

Hi all,  I need your help.

I need to plot a graphic like the one below.  




I was looking on info for it, and I've found that I needed a long data frame. 





This is my data: 


#####################################################


z <- structure(list(anuncios = structure(c(2L, 1L, 2L, 1L),

                                         .Label = c("PPA1 - Carnes",

                                                    

                                                                       "PPA2 - Pastas"), class = "factor"), acciones = structure(c(1L, 

                                                                                                                                   1L, 2L, 2L), .Label = c("comentarios", "compartidos"), class = "factor"), 

                    cantidad = c(131L, 125L, 120L, 69L)),

               row.names = c(NA, -4L

                    ),

               .Names = c("anuncios", "acciones", "cantidad"),

               class = "data.frame")


########################################################


Code:

I've tried this: 



ggplot(z, aes(x=factor(anuncios), fill=factor(acciones))) +

        geom_bar(position="dodge")






But that didn't work as I expecter, both bars are the same hight:








barras.png

Cristel Santos

unread,
Jan 17, 2016, 2:35:30 PM1/17/16
to Omar André Gonzáles Díaz, ggplot2
Hi Omar, 

Try this:

ggplot(z,aes(x=anuncios,y=cantidad, fill=acciones)) + geom_bar(stat="identity",position="dodge”)

regards

Cristel



--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
<barras.png>

Omar André Gonzáles Díaz

unread,
Jan 17, 2016, 3:28:34 PM1/17/16
to ggplot2, oma.go...@gmail.com
Hi Cristel,

thanks, but I get this error message:

Error in +geom_bar(stat = "identity", position = "dodge") : 
  invalid argument to unary operator

Cristel Santos

unread,
Jan 17, 2016, 4:51:08 PM1/17/16
to Omar André Gonzáles Díaz, ggplot2
Hi Omar,

Make sure the code is on the same line or make it so “+” is at the end of the first line like so: 

ggplot(z,aes(x=anuncios,y=cantidad, fill=acciones)) +

geom_bar(stat="identity",position="dodge”)

Let me know if it solves the problem. 

Cristel 
Reply all
Reply to author
Forward
0 new messages