Adding legend to a multi-histogram ggplot

user1701545

I'm trying to add a legend to a ggplot of two histograms, that may overlap and therefore would like to have them slightly transparent:

library(ggplot2)
set.seed(1)
plot.df <- data.frame(x=c(rnorm(1000,30,1),rnorm(10000,40,5)),
                      group=c(rep("a",1000),rep("b",10000)))

using:

ggplot(plot.df,aes(x=x,fill=factor(group)))+ 
  geom_histogram(data=subset(plot.df,group=='a'),fill="red",alpha=0.5)+
  geom_histogram(data=subset(plot.df,group=='b'),fill="darkgray",alpha=0.5)+
  scale_colour_manual(name="group",values=c("red","darkgray"),labels=c("a","b"))+scale_fill_manual(name="group",values=c("red","darkgray"),labels=c("a","b"))

but all I get is:

enter image description here

What's missing?

Psidom

Instead of plotting the two hisgrams separately, you can specify the fill parameter in the mapping as the group variable, in which case the legend will be automatically generated.

ggplot(plot.df, aes(x=x, fill = group)) + 
  geom_histogram(alpha = 0.5) + 
  scale_fill_manual(name="group",values=c("red","darkgray"),labels=c("a","b"))

enter image description here

Borrowed from here, the trick lies in setting up the fill parameter in the mapping(i.e aes here) of each of the histogram plot and then you can use scale_fill_manual normally:

ggplot(plot.df,aes(x=x))+ 
    geom_histogram(data=subset(plot.df,group=='a'),aes(fill=group),alpha=0.5)+
    geom_histogram(data=subset(plot.df,group=='b'),aes(fill=group),alpha=0.5)+
    scale_fill_manual(name="group", values=c("red","darkgray"),labels=c("a","b"))

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Adding manual legend in ggplot

From Dev

adding values to ggplot legend

From Dev

Adding external legend to ggplot

From Dev

Adding a legend to an histogram plot that has several stat_bin layers using ggplot2

From Dev

Add legend to ggplot histogram with overlayed density plots

From Dev

Creating multi column legend in ggplot

From Dev

Adding a legend to the ggplot map with shape

From Dev

Adding legend in ggplot2

From Dev

Adding a bar chart to a ggplot2 legend

From Dev

Adding legend to a single line chart using ggplot

From Dev

Adding a legend to a ggplot - invalid color name

From Dev

Adding a manual legend in ggplot2?

From Dev

Adding a point to the ggplot messes up the legend

From Dev

R - Adding legend to ggplot graph for regression lines

From Dev

Adding a bar chart to a ggplot2 legend

From Dev

adding value stored in a variable as a legend in ggplot

From Dev

Adding a density line to a histogram with count data in ggplot2

From Dev

Adding a density line to a histogram with multiple variables in ggplot2

From Dev

Left align multi-row ggplot2 legend

From Java

Adding greek symbols to legend ggplot (more than one)

From Dev

Adding legend title and main title in ggplot2

From Dev

Adding legend to ggplot2 with multiple lines on plot

From Dev

Adding a custom legend to a blank ggplot2 line graph

From Dev

R adding legend and directlabels to ggplot2 contour plot

From Dev

adding custom ggplot legend to dashed lines and confidence bands

From Dev

Adding legend to ggplot when lines were added manually

From Dev

Adding a legend to ggplot when using stat_function

From Dev

Adding a custom legend to a blank ggplot2 line graph

From Dev

R adding legend and directlabels to ggplot2 contour plot

Related Related

  1. 1

    Adding manual legend in ggplot

  2. 2

    adding values to ggplot legend

  3. 3

    Adding external legend to ggplot

  4. 4

    Adding a legend to an histogram plot that has several stat_bin layers using ggplot2

  5. 5

    Add legend to ggplot histogram with overlayed density plots

  6. 6

    Creating multi column legend in ggplot

  7. 7

    Adding a legend to the ggplot map with shape

  8. 8

    Adding legend in ggplot2

  9. 9

    Adding a bar chart to a ggplot2 legend

  10. 10

    Adding legend to a single line chart using ggplot

  11. 11

    Adding a legend to a ggplot - invalid color name

  12. 12

    Adding a manual legend in ggplot2?

  13. 13

    Adding a point to the ggplot messes up the legend

  14. 14

    R - Adding legend to ggplot graph for regression lines

  15. 15

    Adding a bar chart to a ggplot2 legend

  16. 16

    adding value stored in a variable as a legend in ggplot

  17. 17

    Adding a density line to a histogram with count data in ggplot2

  18. 18

    Adding a density line to a histogram with multiple variables in ggplot2

  19. 19

    Left align multi-row ggplot2 legend

  20. 20

    Adding greek symbols to legend ggplot (more than one)

  21. 21

    Adding legend title and main title in ggplot2

  22. 22

    Adding legend to ggplot2 with multiple lines on plot

  23. 23

    Adding a custom legend to a blank ggplot2 line graph

  24. 24

    R adding legend and directlabels to ggplot2 contour plot

  25. 25

    adding custom ggplot legend to dashed lines and confidence bands

  26. 26

    Adding legend to ggplot when lines were added manually

  27. 27

    Adding a legend to ggplot when using stat_function

  28. 28

    Adding a custom legend to a blank ggplot2 line graph

  29. 29

    R adding legend and directlabels to ggplot2 contour plot

HotTag

Archive