Plot Histogram with Multiple Different Colors in R (2 Examples)

This tutorial demonstrates how to plot a histogram with multiple colors in the R programming language.

Example Data

data(iris)                                                   # Load example data
head(iris)
#   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# 1          5.1         3.5          1.4         0.2  setosa
# 2          4.9         3.0          1.4         0.2  setosa
# 3          4.7         3.2          1.3         0.2  setosa
# 4          4.6         3.1          1.5         0.2  setosa
# 5          5.0         3.6          1.4         0.2  setosa
# 6          5.4         3.9          1.7         0.4  setosa

Example 1: Creating Base R Histogram with Multiple Colors

iris_breaks <- hist(iris$Sepal.Length)$breaks                # Get breaks of histogram
iris_colors <- rep("blue", length(iris_breaks))              # Define colors of histogram
iris_colors[iris_breaks > 5 & iris_breaks <= 6] <- "green"
iris_colors[iris_breaks > 6 & iris_breaks <= 7] <- "pink"
iris_colors[iris_breaks > 7] <- "red"
hist(iris$Sepal.Length,                                      # Draw histogram with colors
     breaks = iris_breaks,
     col = iris_colors)

r graph figure 1 plot histogram multiple different colors r

Example 2: Creating ggplot2 Histogram with Multiple Colors

install.packages("ggplot2")                                  # Install ggplot2 package
library("ggplot2")                                           # Load ggplot2
ggplot_groups <- rep("Group 1", nrow(iris))                  # Define colors of histogram
ggplot_groups[iris$Sepal.Length > 5 & iris$Sepal.Length <= 6] <- "Group 2"
ggplot_groups[iris$Sepal.Length > 6 & iris$Sepal.Length <= 7] <- "Group 3"
ggplot_groups[iris$Sepal.Length > 7] <- "Group 4"
ggplot_data <- data.frame(Sepal.Length = iris$Sepal.Length,  # Construct data frame for ggplot2 plot
                          ggplot_groups = ggplot_groups)
head(ggplot_data)                                            # Display head of data frame
#   Sepal.Length ggplot_groups
# 1          5.1       Group 2
# 2          4.9       Group 1
# 3          4.7       Group 1
# 4          4.6       Group 1
# 5          5.0       Group 1
# 6          5.4       Group 2
my_plot <- ggplot(ggplot_data,                               # Draw histogram with colors
                  aes(x = Sepal.Length,
                      fill = ggplot_groups)) +
  geom_histogram()
my_plot

r graph figure 2 plot histogram multiple different colors r

my_plot +                                                    # Change colors in histogram
  scale_fill_manual(values = c("Group 1" = "blue",
                               "Group 2" = "green",
                               "Group 3" = "pink",
                               "Group 4" = "red"))

r graph figure 3 plot histogram multiple different colors r

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top