The following data frame contains a column with two normal distributions with different mean and same variance and a categorical variable representing which observations belong to each distribution.
geom_histogram
Fill
In order to create a histogram by group in ggplot2 you will need to input the numerical and the categorical variable inside aes
and use geom_histogram
as follows.
Colour
You can also set the categorical variable to the colour
argument, so the border lines of each histogram will have a different color.
By default, if the histograms overlap, the values will be stacked. Another approach is changing the position to identity
(and setting transparency) or dodge
as in the following examples.
identity position
Setting position = "identity"
is the most common use case, but recall to set a level of transparency with alpha
so both histograms are completely visible.
dodge position
Other option is using position = "dodge"
, which will add an space between each bar so you will be able to see both histograms.
Borders color
If you set fill
inside aes
but not colour
you can change the border color of all histograms as well as its width and linetype with geom_histogram
arguments.
Fill color
If you set colour
but not fill
you can change the fill color of all histograms with the fill
argument of geom_histogram
.
Custom border colors for each group
The borders color can be customized individually with scale_color_manual
. If you want to use a palette you can use scale_color_brewer
, for instance.
Custom fill colors for each group
Similarly to customizing the borders color, the fill colors can be set with scale_fill_manual
or any function supporting fills.
Custom legend title
The legend title is the name of the column of the categorical value of the data set. You can change it with the fill
and/or colour
arguments of the guides
function. As we are passing fill
and colour
to aes
we are setting both or two legends will be displayed.
Custom legend labels
The legend will display the names of the categorical variable by default, but you can change them with scale_color_discrete
and/or scale_fill_discrete
. Note that this will depend to which aes
you set.
Legend position
The position of the legend defaults to the right, but can be changed with the legend.position
component of the theme
function as in the example below.
Remove the legend
Setting position = "none"
the legend will be completely removed.
See also