Problem with "geom_bar" & "scale_fill_manual"

1,185 views
Skip to first unread message

Omar André Gonzáles Díaz

unread,
Dec 29, 2015, 7:40:14 PM12/29/15
to ggplot2
hi all,

I need to plot a faceted plot and assign custom colors to each "ecommerce" in my plot. But the blogs and tuts on this are not very clear to me.

Thus I need your help.

My df has 3 columns: "ecommerce", "rangos", "cantidad".

I'll post the data as a txt file.

=============================================

This is the error I get:

Error in grDevices::col2rgb(colour, TRUE) : 
  invalid color name 'ecommercecolors'

=============================================

Code:

ggplot(tvs.rangos, aes(x = rangos, y = cantidad)) +
        
        geom_bar(stat = "identity", fill = "ecommercecolors") + 
        scale_colour_manual("ecommercecolors",
                values = c("linio" = "orange","ripley" = "blue","falabella" = "green")) +
        labs(title = "Marcas de TV por rangos de precios",
             x = "rangos de precios", y = "cantidad de tvs") +
        facet_wrap(~ ecommerce, nrow = 3) +
        theme(axis.text.x = element_text(colour="grey10",size=16,hjust=.5,vjust=.5,face="bold"),
              axis.text.y = element_text(colour="grey10",size=18,,hjust=1,vjust=0,face="plain"),  
              axis.title.x = element_text(colour="grey40",size=16,angle=0,hjust=.5,vjust=0,face="plain"),
              axis.title.y = element_text(colour="grey40",size=16,angle=90,hjust=.5,vjust=.5,face="plain"),
              plot.title = element_text(size = 18,vjust=4),
              legend.title = element_text(colour="grey40", size=16, face="bold"),
              legend.text = element_text(colour="grey10", size=16, face="bold"),
              strip.text.x = element_text(size = 20, angle = 0))



tvs-rangos.txt

Dennis Murphy

unread,
Dec 31, 2015, 6:41:50 AM12/31/15
to Omar André Gonzáles Díaz, ggplot2
Hi:

The first reason why you got no response was because your dput
triggered an error when read into R:

> tv.rangos <- structure(list(ecommerce = structure(c(3L, 3L, 3L, 3L, 3L, 1L,
+ 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("linio",
+ "ripley", "falabella"), class = c("ordered", "factor")), rangos =
structure(c(2L,
+ 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L
+ ), .Label = c("< S/.500", "S/.500 - S/.1500", "S/.1500 - S/.2500",
+ "S/.2500 - S/.3500", "S/.3500 - S/.4500", "> S/.4,500"), class = c("ordered",
+ "factor")), cantidad = c(6L, 16L, 9L, 6L, 13L, 6L, 114L, 87L,
+ 44L, 33L, 39L, 1L, 17L, 21L, 8L, 8L, 24L)), row.names = c(NA,
+ -17L), vars = list(ecommerce), drop = TRUE, .Names = c("ecommerce",
+ "rangos", "cantidad"), class = c("grouped_df", "tbl_df", "tbl",
+ "data.frame"))
Error in structure(list(ecommerce = structure(c(3L, 3L, 3L, 3L, 3L, 1L, :
object 'ecommerce' not found

The culprit appears to be the code "vars = list(ecommerce)", which
must exist in your session but wasn't exported. After removing the
code between 'vars =' up to 'class =', I managed to get something that
was workable. Hint: check that your data and code don't trigger
irrelevant errors in a fresh R session before sending it to a help
list.

Secondly, there are two reasons why you got the error in the ggplot()
call: (i) you didn't map the fill aesthetic to one of your variables
(you set it to a character string instead); (ii) there is no variable
named ecommercecolors in your data. Since you're only using one geom,
you may as well map the fill aesthetic in the ggplot() call.

Other edits:

* you used scale_colour_manual() when you meant to use
scale_fill_manual() (a common mistake);
* I split the legend title over two lines to give more space to the
actual charts;
* your x-labels ran all over each other in the bar chart, so I flipped
the axes to make the labels more readable.

This is what worked for me (R-3.2.3, ggplot2-2.0.0):

library(ggplot2)

tv.rangos <- structure(list(ecommerce = structure(c(3L, 3L, 3L, 3L, 3L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("linio",
"ripley", "falabella"), class = c("ordered", "factor")), rangos =
structure(c(2L,
3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L
), .Label = c("< S/.500", "S/.500 - S/.1500", "S/.1500 - S/.2500",
"S/.2500 - S/.3500", "S/.3500 - S/.4500", "> S/.4,500"), class = c("ordered",
"factor")), cantidad = c(6L, 16L, 9L, 6L, 13L, 6L, 114L, 87L,
44L, 33L, 39L, 1L, 17L, 21L, 8L, 8L, 24L)), row.names = c(NA,
-17L), class = c("grouped_df", "tbl_df", "tbl", "data.frame"))


ggplot(tv.rangos, aes(x = rangos, y = cantidad, fill = ecommerce)) +
geom_bar(stat = "identity") +
scale_fill_manual("ecommerce\ncolors",
values = c("linio" = "orange","ripley" =
"blue","falabella" = "green")) +
labs(title = "Marcas de TV por rangos de precios",
x = "rangos de precios", y = "cantidad de tvs") +
facet_wrap(~ ecommerce, nrow = 3) +
coord_flip() +
theme(axis.text.x =
element_text(colour="grey10",size=16,hjust=.5,vjust=.5,face="bold"),
axis.text.y =
element_text(colour="grey10",size=18,,hjust=1,vjust=0,face="plain"),
axis.title.x =
element_text(colour="grey40",size=16,angle=0,hjust=.5,vjust=0,face="plain"),
axis.title.y =
element_text(colour="grey40",size=16,angle=90,hjust=.5,vjust=.5,face="plain"),
plot.title = element_text(size = 18,vjust=4),
legend.title = element_text(colour="grey40", size=16,
face="bold"),
legend.text = element_text(colour="grey10", size=16, face="bold"),
strip.text.x = element_text(size = 20, angle = 0))


Dennis<div id="DDB4FAA8-2DD7-40BB-A1B8-4E2AA1F9FDF2"><table
style="border-top: 1px solid #aaabb6; margin-top: 10px;">
<tr>
<td style="width: 105px; padding-top: 15px;">
<a href="https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail"
target="_blank"><img
src="https://ipmcdn.avast.com/images/logo-avast-v1.png" style="width:
90px; height:33px;"/></a>
</td>
<td style="width: 470px; padding-top: 20px; color: #41424e;
font-size: 13px; font-family: Arial, Helvetica, sans-serif;
line-height: 18px;">This email has been sent from a virus-free
computer protected by Avast. <br /><a
href="https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail"
target="_blank" style="color: #4453ea;">www.avast.com</a>
</td>
</tr>
</table><a href="#DDB4FAA8-2DD7-40BB-A1B8-4E2AA1F9FDF2" width="1"
height="1"></a></div>
> --
> --
> 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.

Omar André Gonzáles Díaz

unread,
Jan 2, 2016, 12:13:18 PM1/2/16
to Dennis Murphy, ggplot2
Hi Dennis, happy new year. 

Thank you very much for your effort. I got the result I was expecting. And it looks nicer with the coord_flip function applied on.

From your answer, I can see 2 mistakes from my part:

-I've used "fill" in the geom_bar function, instead of in the first call to ggplot function.
-I was using scale_colour_manual instead of scale_fill_manual.

Thanks again for your time.
Reply all
Reply to author
Forward
0 new messages