# Load necessary libraries
library(ggplot2)
library(here)
<-readRDS(here("databases/weo_usa.rds")) weo_usa
Customizing Line Width and Type
Introduction
R provides extensive options for customizing the appearance of plots, including adjusting the line width and line types. This tutorial will guide you through this process.
WEO USA data
We will use the same data frame as in the previous section.
We will start with a line chart of US unemployment starting in 1980.
# Using the same sample data as barplot
# Filter the data to include only years >= 1980
<- subset(weo_usa, year >= 1980)
weo_usa
# Create a line chart
plot(weo_usa$year,weo_usa$lur, type = "l", main = "US: Unemployment", xlab = "", ylab = "Percent", col = "red")
Adjusting Line Width
The line width in R plots can be modified using the lwd
parameter. It specifies the line width relative to the default, which is usually 1.
Here’s an example of how to adjust the line width:
# Using the same sample data as barplot
# Create a line chart
plot(weo_usa$year,weo_usa$lur, type = "l", main = "US: Unemployment", xlab = "", ylab = "Percent", col = "red", lwd=4)
Using Line Types
The line type can be specified using the lty
parameter. R has several predefined line types:
lty = 1
: Solid linelty = 2
: Dashed linelty = 3
: Dotted linelty = 4
: Dotdash linelty = 5
: Longdash linelty = 6
: Twodash line
Here’s an example how you can apply different line types to the unemployment data plot:
# Using the same sample data as barplot
# Create a line chart
plot(weo_usa$year,weo_usa$lur, type = "l", main = "US: Unemployment", xlab = "", ylab = "Percent", col = "red",lwd=2,lty=4)
Custom Line Types
Apart from the predefined line types, R allows for custom line types with a string of up to eight numbers, which represent the length of the line segments and gaps in the pattern. For example, lty = "42"
will create a pattern with 4 units of line followed by 2 units of gap.
We will start with a line chart of US unemployment.
# Using the same sample data as barplot
# Create a line chart
plot(weo_usa$year,weo_usa$lur, type = "l", main = "US: Unemployment", xlab = "", ylab = "Percent", col = "red",lty=42,lwd=3)
Summary
In this tutorial, we’ve demonstrated how to customize line width and line types in R using a practical example of unemployment rates over the years. B