Guest User

Plotting Omron weight/body-fat data in R with ggplot2

a guest
Apr 12th, 2018
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 4.74 KB | None | 0 0
  1. weight <- read.csv("~/selfexperiment/weight.csv", colClasses=c("Date", rep("numeric", 7)))
  2. weight <- weight[weight$Date>=as.Date("2014-10-08") & weight$Date<=Sys.Date(),]
  3. head(weight)
  4. ##           Date Weight.kg Weight.BMI Weight.body.fat.percent Weight.muscle.percent Weight.resting.metabolism.kcal Weight.body.age.years
  5. ## 820 2014-10-08     89.04       27.6                    27.1                  35.5                           1897                    53
  6. ## 821 2014-10-09     88.95       27.5                    27.9                  35.0                           1892                    53
  7. ## 822 2014-10-10     88.22       27.2                    28.6                  34.5                           1876                    53
  8. ## 823 2014-10-11     88.68       27.3                    28.8                  34.4                           1881                    53
  9. ## 824 2014-10-12     89.09       27.6                    28.3                  34.8                           1895                    54
  10. ## 825 2014-10-13     90.58       27.8                    28.7                  34.6                           1904                    54
  11. ##     Weight.visceral.fat.scale
  12. ## 820                        10
  13. ## 821                        10
  14. ## 822                        10
  15. ## 823                        10
  16. ## 824                        10
  17. ## 825                        10
  18.  
  19. # daily weight gain/loss
  20. weight$Weight.kg.dailydelta <- c(NA, diff(weight$Weight.kg, lag=1))
  21.  
  22. # absolute muscle & fat changes
  23. weight$Weight.muscle.kg   <- weight$Weight.kg * (weight$Weight.muscle.percent   / 100)
  24. weight$Weight.body.fat.kg <- weight$Weight.kg * (weight$Weight.body.fat.percent / 100)
  25.  
  26. dailytodo <- read.csv("~/selfexperiment/dailytodo.csv", colClasses=c("Date", rep("numeric", 8)))
  27. workout <- dailytodo[!is.na(dailytodo$Workout) & dailytodo$Workout,]$Date
  28.  
  29. weight$Workout <- weight$Date %in% workout
  30. weight <- aggregate(cbind(Weight.kg, Weight.kg.dailydelta, Weight.body.fat.percent, Weight.muscle.kg, Weight.muscle.percent, Weight.body.fat.kg) ~ Date, mean, data=weight)
  31.  
  32. # Workout on day N will only affect day N+1's weight data, so shift down:
  33. weight$Workout.previous <- (weight$Date %in% (workout+1))
  34.  
  35. summary(weight)
  36. ##       Date              Weight.kg        Weight.kg.dailydelta   Weight.body.fat.percent Weight.muscle.kg   Weight.muscle.percent
  37. ##  Min.   :2014-10-09   Min.   :82.07500   Min.   :-1.450000000   Min.   :21.67500        Min.   :29.56332   Min.   :32.50000    
  38. ##  1st Qu.:2015-08-07   1st Qu.:89.09000   1st Qu.:-0.135000000   1st Qu.:28.20000        1st Qu.:31.04194   1st Qu.:33.66667    
  39. ##  Median :2016-06-28   Median :92.80000   Median : 0.000000000   Median :29.40000        Median :31.49510   Median :34.15000    
  40. ##  Mean   :2016-07-02   Mean   :91.81702   Mean   : 0.001386556   Mean   :29.00896        Mean   :31.51515   Mean   :34.35200    
  41. ##  3rd Qu.:2017-05-23   3rd Qu.:94.21500   3rd Qu.: 0.133333333   3rd Qu.:30.20000        3rd Qu.:31.97816   3rd Qu.:34.80000    
  42. ##  Max.   :2018-04-11   Max.   :98.06667   Max.   : 1.540000000   Max.   :32.03333        Max.   :33.79257   Max.   :38.57500    
  43. ##  Weight.body.fat.kg Workout.previous
  44. ##  Min.   :18.10410   Mode :logical  
  45. ##  1st Qu.:25.23316   FALSE:980      
  46. ##  Median :27.40273   TRUE :196      
  47. ##  Mean   :26.68223   NA's :0        
  48. ##  3rd Qu.:28.35467                  
  49. ##  Max.   :30.81967                  
  50.  
  51. library(ggplot2)
  52. library(gridExtra)
  53. ephedrine <- geom_vline(xintercept=as.integer(as.Date("2017-11-22")), color="green")
  54. gym <- geom_vline(xintercept=as.integer(as.Date("2017-10-19")), color="blue")
  55. gym2 <- geom_vline(xintercept=as.integer(as.Date("2017-12-16")), color="blue2")
  56. p1.1 <- qplot(Date, Weight.kg, color=Workout.previous, data=weight) + stat_smooth(aes(group=1)) + theme(legend.position = "none") + ephedrine + gym + gym2
  57. p1.2 <- qplot(Date, Weight.kg.dailydelta, color=Workout.previous, data=weight) + stat_smooth(aes(group=1)) + theme(legend.position = "none") + ephedrine + gym + gym2
  58. p2.1 <- qplot(Date, Weight.body.fat.percent, color=Workout.previous, data=weight) + stat_smooth(aes(group=1)) + theme(legend.position = "none") + ephedrine + gym + gym2
  59. p2.2 <- qplot(Date, Weight.body.fat.kg, color=Workout.previous, data=weight) + stat_smooth(aes(group=1)) + theme(legend.position = "none") + ephedrine + gym + gym2
  60. p3.1 <- qplot(Date, Weight.muscle.percent, color=Workout.previous, data=weight) + stat_smooth(aes(group=1)) + theme(legend.position = "none") + ephedrine + gym + gym2
  61. p3.2 <- qplot(Date, Weight.muscle.kg, color=Workout.previous, data=weight) + stat_smooth(aes(group=1)) + theme(legend.position = "none") + ephedrine + gym + gym2
  62. # grid.arrange(p1, p2.1, p2.2, p3.1, p3.2, ncol=1)
  63. grid.arrange(p1.1, p1.2, p2.1, p2.2, p3.1, p3.2, ncol=1)
Add Comment
Please, Sign In to add comment