Control Line Color & Type in ggplot2 Plot Legend in R (Example)

 

In this article, I’ll show how to modify the lines and colors of a ggplot2 legend in the R programming language.

The content is structured as follows:

Let’s jump right to the example.

 

Example Data, Packages & Basic Plot

As first step, I need to construct some data that we can use in the following examples:

set.seed(965743)                 # Create example data
data <- data.frame(x = 1:20,
                   y = rnorm(100),
                   group = letters[1:5])
head(data)                       # Head of example data
#             x group
# 1  0.36482821     a
# 2  0.29886899     b
# 3 -0.24979381     c
# 4  0.03346469     d
# 5 -1.15812369     e
# 6 -0.63967012     a

The previous RStudio console output shows the structure of the example data – It contains two columns. The first variable consists of random numeric values and the second variable indicates the groups to which the numeric values belong.

If we want to create a graph of our data with the ggplot2 package, we also need to install and load ggplot2:

install.packages("ggplot2")      # Install & load ggplot2 package
library("ggplot2")

Next, we can draw a graph of our data:

ggp <- ggplot(data, aes(x, y,    # Line plot created with ggplot2
                        color = group,
                        linetype = group)) +
  geom_line()
ggp                              # Draw plot

 

r graph figure 1 control line color type ggplot2 legend r

 

The output of the previous code is shown in Figure 1 – It shows a line chart created with the ggplot2 package. Colors and line types of the plot are selected based on the default specifications of the ggplot2 package.

 

Example: Manually Adjust Line Type & Color in ggplot2 Legend

The following R programming code illustrates how to select colors and line types manually. We can change the line styles with the scale_linetype_manual function and the color of each line with the scale_color_manual function. Consider the following R code:

ggp +                            # Change linetype and color manually
  scale_linetype_manual(values = c(rep("solid", 2), rep("dashed", 2), "twodash")) +
  scale_color_manual(values = c("red", "green", "black", "orange", "pink"))

 

r graph figure 2 control line color type ggplot2 legend r

 

Figure 2 illustrates the output of the previously shown code: A line chart with manually specified line types and colors.

 

Video & Further Resources

Have a look at the following video of my YouTube channel. I’m explaining the contents of this article in the video.

 

The YouTube video will be added soon.

 

In addition, you could read some of the other tutorials on my homepage:

 

To summarize: In this tutorial you learned how to adjust line colors and types of a ggplot2 legend in the R programming language. If you have additional questions, let me know in the comments section below. Furthermore, don’t forget to subscribe to my email newsletter to get updates on new articles.

 



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