The post https://atomic-temporary-119062906.wpcomstaging.com/2018/12/27/how-much-did-you-walk-this-year/ describes process to download steps data from Iphone. The same post also describes in detail the method to generate a bubble plot in R using the ggplot2 package.
In the current post we will use the same Iphone steps dataset and generate a radial plot in R.
Radial Plot :
A radial plot can be a great starting point to show data in a circular representation. Since, our steps data is hourly we can use the radial plot to look like a clock. The steps walked each hour each day can be represented as a clock and the number of steps is visualized as distance from the centre. The radial plot is read clockwise and it shows difference in steps between morning and evening as well as monthly.
A radial plot does not have to be a line (as shown in our example), we can use points, symbols and polygons to represent data. One good application of a radial plot with polygons is to show the oil price over time.
The data for steps can be downloaded using an App called QS. I have described in detail the method to download the data in my post – https://atomic-temporary-119062906.wpcomstaging.com/2018/12/27/how-much-did-you-walk-this-year/ .
Following packages are used to generate the radial plot-
library(lubridate) library(dplyr) library(plotrix)
The radial plot is created using the plotrix package in R. We will also use lubridate and dplyr package to clean and transform data. If you have never used these packages chances are that they are not installed on your R. Hence, you will have to install these packages prior to using the library() function.
Following code can be used to install these packages:
walk <- read.csv("Health Data 4.csv", stringsAsFactors = FALSE) colnames(walk) <- c("start","finish","dist","steps") walk %>% select(finish,steps) -> walk walk$finish <- dmy_hm(walk$finish) walk %>% mutate(yr = year(finish), mth= month(finish), day = day(finish), hr = hour(finish)) %>% filter(yr ==2018) -> walk_2018
The read.csv() function is used to import the steps data in R. The colnames() function is used to update the column names to make it easy to understand. Finally, the mutate() function is used create additional columns in our data. These additional columns will allow us to filter, group, and summarize steps data for generating the radial plot.
Once we have the necessary columns ready we will use the group_by() function and summarise() function.
walk_2018 %>% group_by(mth,hr) %>% summarise(ss=sum(steps))->data
The reason behind grouping the mth and hr column is that we would like to aggregate hourly step data by month. We can then plot this data as a radial plot.
The hard part is to understand the data and grouping the data. Generating radial plot is easy using the plotrix package. Following code can be used to generate radial plot for month of January 2018.
par(bg="black", cex.lab=0.8, col.main="white") clock24.plot(subset(data, mth ==1)$ss,subset(data, mth ==1)$hr, main="Jan", line.col =ifelse(data$hr<12,"#e7298a","#8c510a"),lwd=2.5, show.grid.labels = FALSE, label.prop = 1.10,radial.lim=c(0,45000), grid.col ="#084594", grid.bg="#6baed6", boxed.radial = FALSE)
The code above uses clock24.plot() function to generate the radial plot. Note the use of subset() function to filter our data for the month of january. The ifelse() function is also used to color line for AM and PM with separate colors. The clock24.plot() function has other additional attributes which are described in detail under the plotrix documentation. Alternatively we can also type ?clock24.plot in R console window to learn more.
When we execute the code above we get a radial plot for month of January 2018.
We observe that the steps are higher in morning when i am going to work and also when i am coming back from work. We can also see that the summer months have higher steps compared to winter months.
We can now write a loop to plot 12 separate radial plot. But ,R plotting window is small to show all 12 plots at once. Hence, we will generate 12 individual plots and stitch them in Inkscape. We will also use Inkscape to add a legend and some additional text to our plot.
If you like to go one step forward you could also use functions from lubridate package to identify if days are weekdays or weekends. This will allow us to study the step pattern during weekends over time.
Also, we could animate radial plot to make it look even cooler. But all that for next post.
I would be curious to know if you have used radial plot or have com across some interesting applications. Feel free to add comment on this post.