color

16 views
Skip to first unread message

Michel ARNAUD

unread,
Mar 17, 2014, 2:48:40 AM3/17/14
to ggp...@googlegroups.com
Hi
I have a small question concerning the color of bars in geom_bar.

By default the color of the bars of the histogram is blue with dark blue for the low values of Nbre and light blue for the strong values of Nbre. 
1. How to change the color (green instead of the blue)?
2. How to change the order : darkened for the strong values of Nbre and light for the low values of Nbre

This below my code :

TEMP <- structure(list(Annee = c("2004", "2005", "2006", "2007", "2008", 
"2009", "2010", "2011", "2012", "2013"), Nbre = c(877L, 944L, 
946L, 984L, 1238L, 1201L, 1256L, 1217L, 1349L, 1158L)), .Names = c("Annee", 
"Nbre"), row.names = c(NA, -10L), class = "data.frame")

TEMP$Annee <- factor(TEMP$Annee)

library(ggplot2)
# avec ggplot
ggplot(TEMP, aes(x=Annee, y=Nbre)) +
geom_bar(aes(fill = Nbre), stat="identity", width = 0.7) 

Thank you for your help
Michel

Dennis Murphy

unread,
Mar 17, 2014, 3:16:54 AM3/17/14
to Michel ARNAUD, ggplot2
Hi:

This is just something to get you started - you probably want a wider
range of colors than what is shown here. I used scale_fill_gradient2()
below; see its on-line help page for more information:
http://docs.ggplot2.org/current/scale_gradient2.html

You might also consider looking into colorRampPalette() or the
colorspace package for more options with scale_gradientn(). There are
several examples in Hadley's ggplot2 book that show how to define a
palette for use in a suitable scale_gradient*() function. (BTW, greens
and reds are not good choices for color palettes - see the help page
above for an explanation why.

#-----
TEMP <- structure(list(Annee = c("2004", "2005", "2006", "2007", "2008",
"2009", "2010", "2011", "2012", "2013"), Nbre = c(877L, 944L,
946L, 984L, 1238L, 1201L, 1256L, 1217L, 1349L, 1158L)), .Names = c("Annee",
"Nbre"), row.names = c(NA, -10L), class = "data.frame")

TEMP$Annee <- factor(TEMP$Annee)

library(ggplot2)
# avec ggplot
ggplot(TEMP, aes(x=Annee, y=Nbre)) +
geom_bar(aes(fill = Nbre), stat="identity", width = 0.7) +
scale_fill_gradient2(high = "darkgreen", low = "greenyellow", mid = "green")
#----


Dennis
> --
> --
> 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.

Michel ARNAUD

unread,
Mar 18, 2014, 3:55:19 AM3/18/14
to ggp...@googlegroups.com

Michel ARNAUD

unread,
Mar 18, 2014, 4:05:48 AM3/18/14
to ggp...@googlegroups.com
Merci Dennis et bonne journée
Michel
Reply all
Reply to author
Forward
0 new messages