To plot a line graph in Base R you need:
plot()
functiontype
keyword argument set to ‘l’ for lineHere’s a simple example:
x = c(0, 1, 2, 3, 4, 5, 6)
y = c(0, 1, 4, 9, 16, 25, 36)
plot(x, y, type = "l")
In the above example, the y-values were simply the x-values squared, so we could have achieved the same thing with an equation as opposed to explicit values:
x = 0:6
y = x**2
plot(x, y, type = "l")
If you want to plot additional lines on the same set of axes you will need to use plot()
for the first line and lines()
for the additional lines:
x = 0:6
# Plot a parabola
y = x**2
plot(x, y, type = "l")
# Plot a straight line
y = x
lines(x, y)
The function plot()
can take keyword arguments, some of which are:
main
which sets the main title of the graphxlab
which sets the x-axis labelylab
which sets the y-axis labelHere’s what it looks like:
x = 0:6
# Plot a parabola
y = x**2
plot(
x, y, type = "l", main = "y = x and y = x^2",
xlab = "x-values", ylab = "y-values"
)
# Plot a straight line
y = x
lines(x, y)
Of course, having “x^2” instead of “x²” in the title doesn’t look great. To improve this text formatting we have three options, each with their own pros and cons:
Here are examples of each option:
This is the simplest option: find the symbols you want (eg by Googling them) and copy-paste them into your code:
x <- seq(0, 2, 0.01)
# Plot a parabola
y <- x**2
plot(
x, y, type = "l", main = "β = α and β = α² for 0 ≥ α ≥ 2",
xlab = "Input, α (μs)", ylab = "Output, β (mΩ)"
)
# Plot a straight line
y <- x
lines(x, y)
A slightly more complicated option is to look up the Unicode codes for the symbols and use those prepended by a " to let R know you want to interpret it as Unicode:
x <- seq(0, 2, 0.01)
# Plot a parabola
y <- x**2
plot(
x, y, type = "l", main = "\U03B2 = \U03B1 and \U03B2 = \U03B1\U00B2 for 0 \U2265 \U03B1 \U2265 2",
xlab = "Input, \U03B1 (\U03BCs)", ylab = "Output, \U03B2 (m\U03A9)"
)
# Plot a straight line
y <- x
lines(x, y)
By using the latex2exp
package, you can use the full power of Latex to generate proper-looking labels. Note, however, that backslashes have a special meaning in R and you need to escape this by using a second backslash (an escaping backslash) in order to allow the first backslash be interpreted as such by Latex:
library(latex2exp)
x <- seq(0, 2, 0.01)
# Plot a parabola
y = x**2
plot(
x, y, type = "l", main = TeX("\\beta = \\alpha and \\beta = \\alpha$^2$ for 0\\geq\\alpha\\geq{}2"),
xlab = TeX("Input, \\alpha (\\mu{}s)"), ylab = TeX("Output, \\beta (m\\Omega)")
)
# Plot a straight line
y = x
lines(x, y)
Specify the line width with the lwd
keyword argument:
age = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)
large_dog = c(15, 24, 28, 32, 36, 45, 50, 55, 61, 66, 72, 77, 82, 88, 93, 120)
med_dog = c(15, 24, 28, 32, 36, 42, 47, 51, 56, 60, 65, 69, 74, 78, 83, 87)
small_dog = c(15, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 68, 72, 76, 80)
plot(
age, large_dog, type = "l", lwd = 5,
main = "How Old is a Dog in Dog Years?",
xlab = "Age in Human Years", ylab = "Age in Dog Years"
)
lines(age, med_dog, lwd = 3)
lines(age, small_dog, lwd = 1)
FYI, the above plot comes from this page.
Line width is specified in pixels:
So lwd = 1
implies a line width of one pixel, 0.75 points or 0.26 mm.
For more details on customising lines, see here.
A legend can be added by using the legend()
function:
age = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)
large_dog = c(15, 24, 28, 32, 36, 45, 50, 55, 61, 66, 72, 77, 82, 88, 93, 120)
med_dog = c(15, 24, 28, 32, 36, 42, 47, 51, 56, 60, 65, 69, 74, 78, 83, 87)
small_dog = c(15, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 68, 72, 76, 80)
plot(
age, large_dog, type = "l", lwd = 5,
main = "How Old is a Dog in Dog Years?",
xlab = "Age in Human Years", ylab = "Age in Dog Years"
)
lines(age, med_dog, lwd = 3)
lines(age, small_dog, lwd = 1)
legend(
"topleft", legend = c("Large dog", "Medium dog", "Small dog"),
lwd = c(1, 3, 5)
)
Up until now all the plots have been plotting vectors of data. If we instead use data frames it can sometimes make things easier, but note that a data frame’s columns are vector objects anyway so it doesn’t make much difference.
data = data.frame(
matchweek = 1:38,
points = c(
3, 6, 7, 10, 13, 16, 19, 20, 23, 26, 29, 32, 35, 38, 41, 41, 44, 44,
44, 47, 50, 53, 56, 56, 62, 65, 65, 68, 71, 74, 74, 80, 80, 83, 89, 92,
95, 98
)
)
plot(
x = data$matchweek, y = data$points, type = "l", col = "#6CADDF",
main = "Premier League 2018-19 Log Points",
xlab = "Match Week", ylab = "Points", lwd = 3
)
legend("topleft", legend = "Manchester City", col = "#6CADDF", lwd = 3)
For this example, the raw data will be imported from a CSV file:
# Import raw data
data = read.csv(
"https://github.com/rowannicholls/rowannicholls.github.io/blob/master/R/graphs/base/Points.csv?raw=true",
check.names = FALSE
)
Now, instead of just plotting Manchester City’s points we can plot all the teams’ points for each match week of the 2018-19 season:
# Initialise the plot
plot(
x = 1:38, y = data[["Man City"]], type = "l", col = "#6CADDF",
main = "Premier League 2018-19 Log Points",
xlab = "Match Week", ylab = "Points", lwd = 3
)
# Create the named list of team colours
teams = c(
"Arsenal", "Bournemouth", "Brighton", "Burnley", "Cardiff", "Chelsea",
"Crystal Palace", "Everton", "Fulham", "Huddersfield", "Leicester",
"Liverpool", "Man City", "Man Utd", "Newcastle", "Southampton", "Spurs",
"Watford", "West Ham", "Wolves"
)
team_colours = c(
"#ff0000", "#8b0304", "#005daa", "#80bfff", "#224781", "#0000dd",
"#0a4af5", "#274488", "#000000", "#176fc0", "#0101e8", "#dd0000",
"#6caddf", "#e80909", "#000000", "#ed1a3b", "#132257", "#fbee23",
"#7f0000", "#fdbc02"
)
names(team_colours) = teams
# Plot
for (team in colnames(data)) {
lines(1:38, data[[team]], lwd = 3, col = team_colours[[team]])
}
legend("topleft", legend = teams, col = team_colours, lwd = 3, cex = 0.6)
Save your plot to your computer as a PNG image using png("Name of Plot.png")
or as a PDF using pdf("Name of Plot.pdf")
. Note that, whichever you choose, you need to put it before the code that plots your graph.