In the post How to build a histogram in R we learned how to plot a basic histogram by calling the hist() function, and then a new histogram was created with the automatic computation of the range of the axes and size of bars. The default appearance of the histogram would be sufficient if we are just performing a quick analysis of the data. But in a real presentation with a dozen of other charts on the same plot, the information that we are communicating would be lost in a sea of black and gray lines.

To add colors to our histogram is a very simple task. All we have to do is to pass a character value to the col argument of the hist() function and the bars of the histogram will be plot using that color:

hist(iris$Sepal.Length, col='blue')


Observe that we used the ‘blue’ string to reference the color that should be used in the plot. The R programming language defines more than 600 color names that can be passed as argument to the col argument. We can list them by calling the colors() function, which returns a character array containing the color names:


[1]  "white"           "aliceblue"     "antiquewhite"  "antiquewhite1"       
[5]  "antiquewhite2"   "antiquewhite3" "antiquewhite4" "aquamarine"          
[9]  "aquamarine1"     "aquamarine2"   "aquamarine3"   "aquamarine4"
[13] "azure"           "azure1"        "azure2"        "azure3"              
[17] "azure4"          "beige"         "bisque"        "bisque1"
[21] "bisque2"         "bisque3"       "bisque4"       "black"
[25] "blanchedalmond"  "blue"          "blue1"                             

If none of the default colors is suitable to our plot, we also can use custom colors with the col argument to fill our histogram. In this case, we have to use the hexadecimal notation to represent RGB colors (#RRGGBB):

hist(iris$Sepal.Length, col = '#C8E6C9')


Another interesting possibility is to assign a different color to each bar. We can achieve this by passing an array of characters as argument to the col parameter. In this example I created my own palete of colors using an array of named elements:

pastel.colors <- c(

Then we plot the histogram using one color to each bar by passing the array as argument to the col parameter:

hist(iris$Sepal.Length, col = pastel.colors)


And that’s it! In future posts we are going to learn more about histograms customization and how to apply colors in other types of charts.