A circular line chart may be useful when plotting cyclic dataseries resulting from the combination of a measurement variable and a time-related numerical/continuous variable. It usually helps highlighting a cyclic pattern, a repetition (or absence of repetition) in time. In principle, a circular line chart is a regular line chart which is projected into a polar coordinate system.
Before going any further, if you are not so familiar with line plots or polar coordinate systems, have a quick look at these two pages:
Here we will plot the temperatures (response variable) recorded hourly (predictor variable 1) at Lygra, Vestland, between Apr. 15th and 22nd, 2019 (predictor variable 2).
The dataframe for this tutorial is as follows:
# dataframe
df <- data.frame(day, time, temperature)
# structure of the dataframe
str(df)
## 'data.frame': 168 obs. of 3 variables:
## $ day : Factor w/ 7 levels "Monday","Tuesday",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ time : Factor w/ 24 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10 ...
## $ temperature: num 3.7 1.8 1.2 1.3 0.8 0.8 0.9 4.5 6.9 8.6 ...
Using the coding strategy presented here, we can start by building a multiple line chart. 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, temperature, day)
. Our plan is thus to:
time
on the X-axis,temperature
on the Y-axis,group=
and color=
to display the data in colored lines based on the predictor variable day
,geom_line()
.The code for the plot is as follows:
ggplot(df, aes(x = time, y = temperature, group = day, color = day)) +
geom_line()
Then, to transform this regular line plot into a circular line plot, we project it into a polar coordinate system with coord_polar()
:
ggplot(df, aes(x = time, y = temperature, group = day, color = day)) +
geom_line() +
coord_polar()
This plot is almost acceptable, but the gap between days (which is materialized by the absence of line between 24 and 1 every day) is suboptimal. One solution is to use geom_polygon()
instead of geom_line()
as geom_polygon()
links the first and the last data points of a series with a segment of corresponding color. Note that geom_polygon()
must (in our case) be used with the argument fill = NA
otherwise the polygone shape resulting from that code will not allow to see the overlapping series correctly:
ggplot(df, aes(time, temperature, group=day, color=day)) +
geom_polygon(fill = NA) +
coord_polar()
Finally we may improve the global look of the plot by increasing the thickness of the lines with size=
and by applying a nicer palette of colors such as viridis:
ggplot(df, aes(time, temperature, group=day, color=day)) +
geom_polygon(size=1.2, fill = NA) +
scale_color_viridis_d() +
coord_polar()
In this section, you will learn how to set/modify all the necessary elements that make a plot complete and comprehensible. Such elements are: