library(readr)
fitbit_data <- read_csv('Tamang_Min_Fitbit.csv')
## Parsed with column specification:
## cols(
## Date = col_character(),
## Month = col_character(),
## Weight = col_double(),
## Calories_Burned = col_number(),
## Steps = col_number(),
## Minutes_Sedentary = col_number(),
## Minutes_Lightly_Active = col_double(),
## Minutes_Fairly_Active = col_double(),
## Minutes_Very_Active = col_double(),
## Activity_Calories = col_number(),
## Minutes_Asleep = col_double(),
## Minutes_Awake = col_double(),
## Number_Awakenings = col_double(),
## Time_In_Bed = col_double(),
## Minutes_REM_Sleep = col_character(),
## Minutes_Light_Sleep = col_character(),
## Minutes_Deep_Sleep = col_character()
## )
#Convert date character to date type
fitbit_data$Date <- as.Date(fitbit_data$Date, format = "%m/%d/%Y")
fitbit_data$Month <- factor(fitbit_data$Month)
head(fitbit_data)
#check data types
colnames(fitbit_data)
## [1] "Date" "Month" "Weight"
## [4] "Calories_Burned" "Steps" "Minutes_Sedentary"
## [7] "Minutes_Lightly_Active" "Minutes_Fairly_Active" "Minutes_Very_Active"
## [10] "Activity_Calories" "Minutes_Asleep" "Minutes_Awake"
## [13] "Number_Awakenings" "Time_In_Bed" "Minutes_REM_Sleep"
## [16] "Minutes_Light_Sleep" "Minutes_Deep_Sleep"
str(fitbit_data)
## Classes 'spec_tbl_df', 'tbl_df', 'tbl' and 'data.frame': 195 obs. of 17 variables:
## $ Date : Date, format: "2019-06-09" "2019-06-10" ...
## $ Month : Factor w/ 7 levels "August","December",..: 4 4 4 4 4 4 4 4 4 4 ...
## $ Weight : num 175 175 175 175 175 ...
## $ Calories_Burned : num 2689 2726 2867 2748 2687 ...
## $ Steps : num 11191 10312 11699 10147 8087 ...
## $ Minutes_Sedentary : num 1194 783 847 712 842 ...
## $ Minutes_Lightly_Active: num 67 176 170 156 205 163 272 232 192 106 ...
## $ Minutes_Fairly_Active : num 38 25 16 5 6 26 24 44 13 17 ...
## $ Minutes_Very_Active : num 73 35 49 44 18 53 55 36 44 71 ...
## $ Activity_Calories : num 1128 1133 1279 1125 1052 ...
## $ Minutes_Asleep : num 371 375 337 397 321 453 366 422 345 343 ...
## $ Minutes_Awake : num 36 38 72 37 80 32 82 22 76 89 ...
## $ Number_Awakenings : num 2 1 38 1 26 2 32 3 35 38 ...
## $ Time_In_Bed : num 426 421 409 439 401 491 448 452 421 432 ...
## $ Minutes_REM_Sleep : chr "N/A" "N/A" "55" "N/A" ...
## $ Minutes_Light_Sleep : chr "N/A" "N/A" "219" "N/A" ...
## $ Minutes_Deep_Sleep : chr "N/A" "N/A" "63" "N/A" ...
## - attr(*, "spec")=
## .. cols(
## .. Date = col_character(),
## .. Month = col_character(),
## .. Weight = col_double(),
## .. Calories_Burned = col_number(),
## .. Steps = col_number(),
## .. Minutes_Sedentary = col_number(),
## .. Minutes_Lightly_Active = col_double(),
## .. Minutes_Fairly_Active = col_double(),
## .. Minutes_Very_Active = col_double(),
## .. Activity_Calories = col_number(),
## .. Minutes_Asleep = col_double(),
## .. Minutes_Awake = col_double(),
## .. Number_Awakenings = col_double(),
## .. Time_In_Bed = col_double(),
## .. Minutes_REM_Sleep = col_character(),
## .. Minutes_Light_Sleep = col_character(),
## .. Minutes_Deep_Sleep = col_character()
## .. )
#load libraries
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.3.0 ✔ purrr 0.3.3
## ✔ tibble 2.1.3 ✔ dplyr 0.8.3
## ✔ tidyr 1.0.0 ✔ stringr 1.4.0
## ✔ ggplot2 3.3.0 ✔ forcats 0.4.0
## ── Conflicts ────────────────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(tabplot)
## Loading required package: bit
## Attaching package bit
## package:bit (c) 2008-2012 Jens Oehlschlaegel (GPL-2)
## creators: bit bitwhich
## coercion: as.logical as.integer as.bit as.bitwhich which
## operator: ! & | xor != ==
## querying: print length any all min max range sum summary
## bit access: length<- [ [<- [[ [[<-
## for more help type ?bit
##
## Attaching package: 'bit'
## The following object is masked from 'package:base':
##
## xor
## Loading required package: ff
## Attaching package ff
## - getOption("fftempdir")=="/var/folders/0h/wwn90c6x6lv8ll7pvrfb_99h0000gn/T//RtmphF7cRg"
## - getOption("ffextension")=="ff"
## - getOption("ffdrop")==TRUE
## - getOption("fffinonexit")==TRUE
## - getOption("ffpagesize")==65536
## - getOption("ffcaching")=="mmnoflush" -- consider "ffeachflush" if your system stalls on large writes
## - getOption("ffbatchbytes")==16777216 -- consider a different value for tuning your system
## - getOption("ffmaxbytes")==536870912 -- consider a different value for tuning your system
##
## Attaching package: 'ff'
## The following objects are masked from 'package:bit':
##
## clone, clone.default, clone.list
## The following objects are masked from 'package:utils':
##
## write.csv, write.csv2
## The following objects are masked from 'package:base':
##
## is.factor, is.ordered
## Loading required package: ffbase
## Registered S3 methods overwritten by 'ffbase':
## method from
## [.ff ff
## [.ffdf ff
## [<-.ff ff
## [<-.ffdf ff
##
## Attaching package: 'ffbase'
## The following objects are masked from 'package:ff':
##
## [.ff, [.ffdf, [<-.ff, [<-.ffdf
## The following objects are masked from 'package:base':
##
## %in%, table
library(RColorBrewer)
library(ggridges)
##
## Attaching package: 'ggridges'
## The following object is masked from 'package:ggplot2':
##
## scale_discrete_manual
library(viridis)
## Loading required package: viridisLite
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(gganimate)
library(ggridges)
library(ggplot2)
summarise(fitbit_data,life_Time_Steps = sum(Steps))
summary(fitbit_data$Steps)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 8015 8764 10171 10545 11329 25717
#line chart
fitbit_data %>% ggplot( aes(x=Date, y= Steps)) +
geom_line( color="red") +
geom_point(shape=21, fill="blue") + scale_x_date(date_labels = "%b\n%Y", date_breaks = "1 month", limits = c(as.Date("2019-06-01"),as.Date("2019-12-31")))

# group by month
ggplot(fitbit_data, aes(x = Date, y = Steps, group = Month, color = 'red' )) + facet_wrap(~Month, scale = "free")+ geom_line() + scale_color_viridis(discrete = TRUE) + ggtitle("Steps Line Chart by Month") + xlab("Month")

library(plotly)
#bubbple plot
bubble_plot <-fitbit_data %>% ggplot( aes(x=Date, y= Steps, size = Weight , color = Month)) +
geom_point(alpha = 0.5) + scale_size(range = c(0.01, 5), name = "Weight") +
scale_fill_viridis(discrete=TRUE, guide=FALSE, option="A") +
theme(legend.position = "none") + scale_x_date(date_labels = "%b\n%Y", date_breaks = "1 month", limits = c(as.Date("2019-06-01"),as.Date("2019-12-31"))) + xlab("Month") + ggtitle('Interactive Bubble Plot')
bubble_plot

ggplotly(bubble_plot)
#Area Chart
area_chart <- fitbit_data %>% ggplot( aes(x=Date, y= Steps, fill = Month)) +geom_area() +
scale_fill_viridis(discrete=TRUE, guide=FALSE, option="A") +
theme() + scale_x_date(date_labels = "%b\n%Y", date_breaks = "1 month", limits = c(as.Date("2019-06-01"),as.Date("2019-12-31"))) + xlab("Month") + ggtitle('Area Chart')
ggplotly(area_chart)
#Ridgeline Plot
ggplot(fitbit_data, aes(x = Steps, y = ordered(Month,levels=month.name), fill = Month)) +
geom_density_ridges() +
theme_ridges() +
theme(legend.position = "none") + ggtitle("Ridgeline Plot") + ylab("Month")
## Picking joint bandwidth of 768

#bar plot
ggplot(fitbit_data, aes(x= Date, y = Steps)) +
geom_bar(stat = "identity", fill = " blue", width = 1) + labs( title = " My Steps", subtitle = "June-Dec 2019") + scale_x_date(date_labels = "%b\n%Y", date_breaks = "1 month", limits = c(as.Date("2019-06-01"),as.Date("2019-12-31")))

#grouped by month
ggplot(fitbit_data, aes(x=ordered(Month, month.name), fill = Month)) + geom_bar() + scale_fill_brewer(palette = "Set1") + theme(legend.position = "none") + xlab("Month") + ylab("Steps")

#lollipop Chart
ggplot(fitbit_data, aes(x =Month, y = Steps)) + geom_segment(aes(Month, xend = Month, y = 0, yend = Steps)) + geom_point(size = 3, color = "red", fill = alpha( "green", 0.4), alpha = 0.4, shape = 21, stroke = 1) + theme_classic()

#Boxplot
box <- ggplot(fitbit_data, aes(x=ordered(Month,month.name), y = Steps)) +
geom_boxplot(width = 0.5, fill = 'orange', alpha = 0.5, outlier.color = "red") + xlab("Month") + ggtitle("Boxplot")
ggplotly(box)
#basic stacked area
ggplot(fitbit_data, aes(x = Date, y = Steps, fill= Month )) + geom_area() +
scale_x_date(date_labels = "%b\n%Y", date_breaks = "1 month", limits = c(as.Date("2019-06-01"),as.Date("2019-12-31")))

#Violin Plot
ggplot(fitbit_data, aes(x = Date, y = Steps)) + geom_violin(fill = 'green', alpha= 0.5, position = 'dodge') + xlab("Month") + ylab("No. of Steps") + ggtitle("Violin Plot")
