An area plot is very similar in appearance to a line plot. It represents a data series based on two numerical variables in the form of a simple line “joining the dots”; however, the area under the line is filled. When more than one response variable is represented, the area plot becomes a multiple or stacked area plot which helps visualize how different components contribute to a total/sum (this will be further explained here).
Let’s see here how to use ggplot()
to draw such a plot. We will consider the following example where precipitations were measured daily at Lygra, Hordaland in January 2020. Here is the dataframe:
# dataframe
df <- data.frame(day, precipitations)
str(df)
## 'data.frame': 31 obs. of 2 variables:
## $ day : int 1 2 3 4 5 6 7 8 9 10 ...
## $ precipitations: num 0.4 9.8 9 3.2 21.6 14.4 20.2 3 3.6 0.2 ...
We map the variables with aes(day, precipitations)
and use geom_area()
to draw the plot:
ggplot(df, aes(day, precipitations)) +
geom_area()
As usual, you may change the look of the plot with a handful of arguments. With fill=
and alpha=
, you may change the color of the area and its transparency, respectively:
ggplot(df, aes(day, precipitations)) +
geom_area(fill="darkblue", alpha=.5)
If you are interested in plotting only one fragment of the data series, you can use ifelse()
in aes()
. You may for instance decide to display only the first 15 days of the month with time<=12
:
ggplot(df, aes(day, precipitations)) +
geom_area(aes(x = ifelse(day<=15, day, NA))) +
xlim(1,31)
## Warning: Removed 16 rows containing missing values (position_stack).
Note the warning message telling you that some data has been excluded (as expected).
Alternatively you can highlight a specific interval of the area plot (for example the period January 10th-23th) by making a two-layered area plot: a first geom_area()
layer shows the whole interval from January 1st to 31st in dark and a second layer geom_area()
shows only the interval January 10th-23th in orange on top of (and thus hiding) the first layer:
ggplot(df, aes(day, precipitations)) +
geom_area(fill="darkslategrey") +
geom_area(aes(x = ifelse(day>=10 & day<=23, day, NA)), fill="orange")
## Warning: Removed 17 rows containing missing values (position_stack).
In this section, you will learn how to set/modify all the necessary elements that make a plot complete and comprehensible. Such elements are: