Reputation: 11
I am trying to add a legend to the graph but it doesn't work. Do you have any ideas ?
Here is my code :
ggplot(data =stats_201507_AF ) +
geom_histogram(aes(gross_ind),fill="dodgerblue3", show.legend =T,bins=25)+
geom_histogram(aes(net_ind),fill="springgreen4",show.legend = T,bins=25) +
geom_histogram(aes(tax_ind),fill="gold2",show.legend = T, bins=25) +
xlab("Indices")+
scale_colour_manual(values=c("dodgerblue3","springgreen4","gold2"))
I wanted a description for every histogram with a corresponding colour.
Thanks a lot in advance
Upvotes: 1
Views: 2875
Reputation: 132989
If you don't want to reshape your data, just do this:
ggplot(iris) +
geom_histogram(aes(x = Sepal.Length, fill = "Sepal.Length"),
position = "identity", alpha = 0.5) +
geom_histogram(aes(x = Sepal.Width, fill = "Sepal.Width"),
position = "identity", alpha = 0.5) +
scale_fill_manual(values = c(Sepal.Length = "blue",
Sepal.Width = "red"))
The key is that you need to map something to fill
inside aes
. Of course, reshaping your data to long format (and actually having a column to map to fill
as a result) is usually preferable.
Upvotes: 3