R ggplot2 Line Plot

The R ggplot2 line Plot or line chart connects the dots in order of the variable present on the x-axis. Let us see how to Create a ggplot line plot in R, Format its colors, add points to the line plot with an example. For this line chart, we use the economics data set provided by the R.

R ggplot2 Line Plot Syntax

The syntax to draw a line plot in R Programming is

geom_line(mapping = NULL, data = NULL, stat = "identity",
  position = "identity", inherit.aes = TRUE, na.rm = FALSE, show.legend = NA,...)

Create a basic R ggplot2 Line Plot

In this example, we create a basic line plot using the ggplot2 package. For this, we are going to use the economics data set provided by the R Studio.

TIP: ggplot2 package not installed by default. Please refer Install R Packages article to install the package.

# Create a R ggplot Line Plot

# Importing the ggplot2 library
library(ggplot2)

# Creating basic Line Plot
ggplot(economics, aes(date, pop)) + 
  geom_line()
R ggplot2 Line Plot 1

NOTE: If you require is to import data from external files, then please refer R Read CSV article in R Programming to import the CSV file.

Change Colors of a Line Plot in R

In this example, we show how to change the line plot color using the color argument

# Create a R ggplot Line Plot

# Importing the ggplot2 library
library(ggplot2)

# Creating basic Line Plot
ggplot(economics, aes(date, pop)) + 
  geom_line(color = "midnightblue")
R ggplot2 Line Plot 2

Arrow argument in an R ggplot2 Line Plot

Use the arrow function to add the arrow at the end of the line plot.

# Create a R ggplot Line Plot

# Importing the ggplot2 library
library(ggplot2)

# Creating basic Line Plot
ggplot(economics, aes(date, pop)) + 
  geom_line(color = "midnightblue", arrow = arrow())
R ggplot2 Line Plot 3

Arrow argument in ggplot2 Line 2

In this example, we show how to change the array type to both sides using the ends, and type argument

# Adding Arrow to an R ggplot Line Plot

# Importing the ggplot2 library
library(ggplot2)

# Creating basic Line Plot
ggplot(economics, aes(date, pop)) + 
  geom_line(color = "midnightblue", 
            arrow = arrow(ends = "both", type = "closed"))
R ggplot2 Line Plot 4

Change Line Type of an R ggplot2 Line plot

In this example, we show you how to change the line type of line plot

# Change the Line Type in an R ggplot Line Plot

# Importing the ggplot2 library
library(ggplot2)

# Creating basic Line Plot
ggplot(economics, aes(date, pop)) + 
  geom_line(color = "midnightblue",
            linetype = 5,
            arrow = arrow(ends = "both", type = "closed"))
R ggplot2 Line Plot 5

TIP: In R programming, 0 = blank, 1 = solid, 2 = dashed, 3 = dotted, 4 = dotdash, 5 = longdash, 6 = twodash. So, you can use numbers or string as the linetype value

Creating R ggplot2 Line plot

In this example, we are using different data to create a line type, so that we can explore more arguments present in the geom_line() function

# Creating Line Type in an R ggplot Line Plot

# Importing the ggplot2 library
library(ggplot2)

# Creating basic Line Plot
ggplot(economics, aes(date, unemploy)) + 
  geom_line(color = "midnightblue")
R ggplot2 Line Plot 6

Change R Line Plot Color using Column Data

In this example. we show you how to add groups and change the line plot colors using column data

# Change color of a R ggplot Line Plot

# Importing the ggplot2 library
library(ggplot2)

# Creating basic Line Plot
ggplot(economics, aes(date, unemploy, group = psavert)) + 
  geom_line(aes(colour = psavert))
R ggplot2 Line Plot 7

Alter Legend position of a R ggplot2 Line plot

By default, ggplot position the legend at the right side of a line plot. In this example, we change the legend position from right to top. Remember, You can use a legend.position = “none” to altogether remove the legend.

# Alter Legend Position of a R ggplot Line Plot

# Importing the ggplot2 library
library(ggplot2)

# Creating basic Line Plot
ggplot(economics, aes(date, unemploy, group = psavert)) + 
  geom_line(aes(colour = psavert)) +
  theme(legend.position = "top")
R ggplot2 Line Plot 8

Assigning names to R ggplot Line plot

We assign names to the ggplot line plot, X-Axis, and Y-Axis using labs function.

# Add Names to R ggplot Line Plot

# Importing the ggplot2 library
library(ggplot2)

# Creating basic Line Plot
ggplot(economics, aes(date, unemploy)) + 
  geom_line(colour = "midnightblue") + 
  labs(title="R GGPLOT LINE PLOT", x="Years", y="Unemployed People")
R ggplot2 Line Plot 9

Add points to R ggplot2 Line plot

In this R ggplot line chart, we add the data points to the line plot using the geom_point() function.

# Add geom_point to R ggplot Line Plot

# Importing the ggplot2 library
library(ggplot2)

# Creating basic Line Plot
ggplot(economics, aes(date, unemploy)) + 
  geom_line(colour = "midnightblue") + 
  geom_point(color = "seagreen") +
  labs(title="R GGPLOT LINE PLOT", x="Years", y="Unemployed People")
R ggplot2 Line Plot 11

Change R ggplot2 Line plot Theme

How to change the default theme of a R ggplot2 line plot?

  • theme_dark(): We use this function to change the line plot default theme to dark. If you type theme_, then R Studio intelligence shows the list of available options. For example, theme_grey()
# Change the R ggplot Line Plot Theme

# Importing the ggplot2 library
library(ggplot2)

# Creating basic Line Plot
ggplot(economics, aes(date, unemploy)) + 
  geom_line(colour = "midnightblue") + 
  labs(title="R GGPLOT LINE PLOT", x="Years", y="Unemployed People") +
  theme_dark()
R ggplot2 Line Plot 10