Coloring Plot by Factor in R (2 Examples)

 

This tutorial shows how to specify graphic colors according to a factor in the R programming language.

The content of the page is structured as follows:

Let’s dive right in!

 

Creation of Exemplifying Data

The following data will be used as basement for this R tutorial:

data <- data.frame(x = 1:100,             # Creating example data
                   y = 1:100,
                   group = LETTERS[1:5])
head(data)                                # Printing example data
#   x y group
# 1 1 1     A
# 2 2 2     B
# 3 3 3     C
# 4 4 4     D
# 5 5 5     E
# 6 6 6     A

Have a look at the previous output of the RStudio console. It shows that our example data has two numeric columns and a factor column that specifies the groups of our data.

 

Example 1: Draw Base R Plot with Colors According to Factor

This Example shows how to use the basic installation of the R programming language to color a plot according to the levels of a factor.

For this, we have to specify the col argument within the plot function to be equal to our grouping factor.

Furthermore, we specify filled points within the plot function by setting the pch argument to be equal to 16 (this is optional).

plot(data$x, data$y,                      # Plotting in Base R
     col = data$group,
     pch = 16)

 

r graph figure 1 coloring factor

 

As visualized in Figure 1, the previous code created a scatterplot in Base R with colors according to the factor variable of our data frame.

 

Example 2: Draw ggplot2 Plot with Colors According to Factor

The following R syntax shows how to draw a ggplot2 graphic with different colors for each factor level.

First, we have to install and load the ggplot2 package:

install.packages("ggplot2")               # Install ggplot2 package
library("ggplot2")                        # Load ggplot2 package

Now, we can draw a ggplot2 xyplot with different colors and a legend as shown below:

ggplot(data, aes(x, y, col = group)) +    # Plotting with ggplot2 package
  geom_point()

 

r graph figure 2 coloring factor

 

As shown in Figure 2, we created a colored ggplot2 graphic with the previous code.

Note that we have drawn scatterplots in the two examples of this tutorial. However, we could apply similar R codes in case we want to draw a histogram, boxplot, line chart, and so on…

 

Video, Further Resources & Summary

Would you like to learn more about graphics in R? Then you might want to watch the following video of my YouTube channel. In the video, I illustrate the R code of this tutorial:

 

The YouTube video will be added soon.

 

In addition, you might read the related tutorials on this website. A selection of articles about plots in R is shown below:

 

In this R tutorial you learned how to change the colors of a graph based on a factor column. Tell me about it in the comments section below, if you have any additional questions.

 

Subscribe to my free statistics newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


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.

Menu
Top