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 :-)