A (multiple) line plot represents two or more data series from a data set in the form of straight lines joining the points or markers (which are themselves represented often as dots, circles, squares, etc.).
Before going any further, if you are not so familiar with line plots, have a quick look at this page:
Let’s use ggplot()
to draw the line plot for a dataset representing the solar irradiance (response variable solarrad
) registered every hour during 24 hours (predictor variable time
) at two locations: Østerbø, Sogn og Fjordane and Lygra, Hordaland (predictor variable location
) on July 23rd, 2017.
The dataframe for this tutorial is as follows:
# dataframe
df <- data.frame(time, location, solarrad)
# structure of the dataframe
str(df)
## 'data.frame': 48 obs. of 3 variables:
## $ time : int 0 1 2 3 4 5 6 7 8 9 ...
## $ location: Factor w/ 2 levels "Lygra","Østerbø": 2 2 2 2 2 2 2 2 2 2 ...
## $ solarrad: num 0 0 0 0 0 0 14 53 183 308 ...
The function to use for drawing the lines is geom_line()
. We must map the variables with the function aes()
which will look more or less like this: aes(time, solarrad, location)
. Our plan is to:
time
on the X-axis,solarrad
on the Y-axis,color=
to split the data for each level of location
,geom_line()
.The code for the plot is as follows:
ggplot(df, aes(x = time, y = solarrad, color = location)) +
geom_line()
Here the data for each location is represented by a colored line.
Alternatively, it is possible to represent the data for each location in the form of a dotted line, a plain line, a dashed line, etc, instead of a specific color. To do so, we replace color=
with linetype=
:
ggplot(df, aes(x = time, y = solarrad, linetype = location)) +
geom_line()
And it is possible to combine bith color and linetype to represent the locations. Here we simply cumulate linetype = location
and color = location
in aes()
:
ggplot(df, aes(x = time, y = solarrad, color = location, linetype = location)) +
geom_line()
As usual, you may tune the thickness of the lines with size=
to make them more visible:
ggplot(df, aes(x = time, y = solarrad, color = location, linetype = location)) +
geom_line(size = 1.3)
It is possible to add shapes to the lines to mark the data points. To do so, we must add a layer with the function geom_point()
. Nothing else is needed as all the necessary info (mapping, grouping) is already mentioned in aes()
:
ggplot(df, aes(x = time, y = solarrad, color = location, linetype = location)) +
geom_line(size = 1) +
geom_point(size = 2)
And if you want to use various shapes (triangle, square, circles, etc) for the markers, add shape = location
to aes()
:
ggplot(df, aes(x = time, y = solarrad, color = location, shape = location)) +
geom_line(size = 1) +
geom_point(size = 2)
In this section, you will learn how to set/modify all the necessary elements that make a plot complete and comprehensible. Such elements are: