# Rework of th weight stats library(ggplot2) # for all the graphics we use ggplot library(plyr) # ddply function call needs this library(gdata) # for date functions # extract the last num lines from the dataset get_latest <- function(data, num) { rows <- nrow(data) cols <- ncol(data) start <- rows - num +1; return( data[start:rows,0:cols] ) } #read the weight data from the file wt_data <- read.table("gewicht.txt", header=TRUE, sep=" ") wt_data$Timestamp <- strptime(paste(wt_data$Datum, wt_data$Zeit), "%d.%m.%y %H:%M") wt_data <- subset(wt_data, select=c(-Datum, -Zeit)) wt_data$month <- format(wt_data$Timestamp, "%Y-%m") wt_data$week <- format(wt_data$Timestamp, "%Y-%W") # plot a graph for the latest 30 days wt_latest <- get_latest(wt_data, 30) weight_plot_latest <- ggplot(wt_latest, aes(x=Timestamp, y=Gewicht)) + geom_line() + geom_point() + xlab("Datum") + labs(title="Entwicklung über die letzten 30 Tage") # plot the all time curve since recordings began curve_all <- ddply(wt_data,"month", summarize, mean_weight=mean(Gewicht)) curve_all$x <- as.numeric(rownames(curve_all))-1 # from 0..n months weight_plot_all <- ggplot(curve_all, aes(x=x,y=mean_weight)) + geom_point() + geom_line(color="red", alpha=0.25) + xlab("Monate seit Aufzeichnungsstart") + ylab("durchschnittliches Gewicht") + labs(title="Durchschnittswerte aller aufgezeichneten Monate") # have a close look at the latest 12 months now <- Sys.Date() year <- as.numeric(getYear(now)) month <- as.numeric(getMonth(now))-11 if (month < 1) { year=year-1 month=12+month } startdate <- as.POSIXlt(ISOdate(year,month,1,hour=1)) # select from 12 months ago wt_range <- subset(wt_data, Timestamp >= startdate) weight_plot_year <- ggplot(wt_range, aes(x=factor(month),y=Gewicht)) + geom_boxplot() + stat_summary(fun.y="mean", geom="point", color="red", size=2) + xlab("Monat") + labs(title="Rückblick auf die letzten 12 Monate")