1

I have a difficulty to set up a legend and change colours in an overlayered histogram. For some reason, when I try to change the colour, the legend is not customised any more. Here is the code without any colour specification, can you please help me?

   # o - o - o  Preparing data o - o - o - o - o - o - o - o - o
   iris$Petal.Length.binary <- NA
   iris$Petal.Length.binary[iris$Petal.Length < 4.5] <- "low length"
   iris$Petal.Length.binary[iris$Petal.Length >= 4.5] <- "high length"
   iris$Petal.Length.binary <- factor(iris$Petal.Length.binary, levels=c("low length", "high length"))

   # o - o - o    Density plot   o - o - o - o - o - o - o - o - o
   Density <- ggplot(iris, aes(Sepal.Length, fill = Petal.Length.binary)) 
   Density2 <- 
(Density + 
 #_____________Change colour density___________ 
        geom_density(alpha = 0.3) + 
 #_____________Position the legend in bottom left___________
 theme(legend.justification=c(0,1), legend.position=c(0,1))  +
 #_____________Customising title___________ http://www.cookbook-r.com/Graphs/Titles_(ggplot2)/
 ggtitle('Title : Example Plot') +
 theme(plot.title = element_text(lineheight=0.9, face="bold")) +
 #_____________Labels___________
 labs(x='Sepal Length', y='Probability Density') +
 #_____________Customising legend___________
 scale_fill_discrete(name = "An irrelevant \nlegend title",
                     breaks=c("low length", "high length"),
                     labels=c("Low \nlength", "High \nlength")) +
 #_____________Increase spacing in legend categories___________     
 theme(legend.key.height=unit(1.5,"line")) +
 theme(legend.key.width=unit(1.2,"line")) ) 
 Density2 

The problematic line that when I add it removes all formatting from the legend is: scale_fill_manual(values=c("#0000FF", "#FF0000"))

And a final question, I like the graph as it is however, the default colours of ggplot2 should be reversed (light blue first, pink second), is there a way to do that? I was not able to find anywhere on the internet the hex code for the default colours.

CORRECT CODE based on Henrik's response

# o - o - o    Density plot   o - o - o - o - o - o - o - o - o
Density <- ggplot(iris, aes(Sepal.Length, fill = Petal.Length.binary)) 
Density2 <- 
   (Density + 
 #_____________Change colour density___________ 
 geom_density(alpha = 0.3) + 
 #_____________Position the legend in bottom left___________
 theme(legend.justification=c(0,1), legend.position=c(0,1))  +
 #_____________Customising title___________ http://www.cookbook-r.com/Graphs/Titles_(ggplot2)/
 ggtitle('Title : Example Plot') +
 theme(plot.title = element_text(lineheight=0.9, face="bold")) +
 #_____________Labels___________
 labs(x='Sepal Length', y='Probability Density') +
 scale_fill_manual(values=c("#0000FF", "#FF0000"),
                   name = "An irrelevant \nlegend title",
                   breaks=c("low length", "high length"),
                   labels=c("Low \nlength", "High \nlength")) +
 #_____________Increase spacing in legend categories___________     
 theme(legend.key.height=unit(1.5,"line")) +
 theme(legend.key.width=unit(1.2,"line")) ) 
 Density2 

And this is how to get access to the 2 colours used by ggplot in the first graph:

gg_color_hue <- function(n) {
  hues = seq(15, 375, length=n+1)
  hcl(h=hues, l=65, c=100)[1:n]
}

n = 2
cols = gg_color_hue(2)
str(cols) # the hex numbers :-)
Pulse
  • 867
  • 5
  • 12
  • 19
  • 1
    When adding `scale_fill_manual` (`Density2 + scale_fill_manual(values=c("#0000FF", "#FF0000"))`) a warning message is generated: "Scale for 'fill' is already present. Adding another scale for 'fill', which will replace the existing scale.". Would you mind to explain what in the warning message that you don't understand? Please also read `?scale_fill_manual`. In the Arguments section you find that you can use "common discrete scale parameters: `name`, `breaks`, `labels`". – Henrik May 19 '15 at 11:34
  • Thank you, you are right. Once I add all the arguments of the legend customisation to the `scale_fill_manual`, I do keep the customised legend and the new colours. `scale_fill_manual(values=c("#0000FF", "#FF0000"), name = "An irrelevant \nlegend title", breaks=c("low length", "high length"), labels=c("Low \nlength", "High \nlength"))` I was wondering whether you know how I could reverse the default colours of the plot used by ggplot. – Pulse May 19 '15 at 11:50
  • 1
    One possibility is to [emulate ggplot2's default color palette for a desired number of colors](http://stackoverflow.com/questions/8197559/emulate-ggplot2-default-color-palette) and `rev`erse the color vector. – Henrik May 19 '15 at 12:06
  • Indeed, thank you very much, I will update my response with the answers – Pulse May 19 '15 at 12:10

0 Answers0