# load libraries library(ggplot2) library(dplyr) library(tidyr) # ggplot format legend_theme <- theme(plot.title = element_text(size=20, hjust = 0.5, face="bold"), axis.title.x = element_text(size=14, face="bold"), axis.title.y = element_text(size=14, face="bold"), legend.title=element_text(size=14), legend.text=element_text(size=12)) theme <- theme(plot.title = element_text(size=20, hjust = 0.5, face="bold"), axis.title.x = element_text(size=14, face="bold"), axis.title.y = element_text(size=14, face="bold"), axis.text.x=element_blank(), axis.text.y=element_blank()) # read data data <- read.csv("acceleration.csv") # print(data) head(data, 10) # markers <- subset(data, Key == 61) # markers <- subset(data, Key %in% c(61)) # with c(x,y,z,...) a list of matching keys markers <- c(t(subset(data, Key == 61, select=Time))) # markers <- subset(data, Key == 61, select=c(Time,Value)) # keep Time and Value column move <- data move <- subset(data, Time > markers[11] & Time < markers[12]) gg <- ggplot(move) gg <- gg + geom_line(aes(x=Time, y=Value, color=factor(Key, labels = c("X", "Y", "Z")))) # with factor color is taken discrete gg <- gg + labs(title = "Acceleration", x = "Time (s)", y = "Acceleration (m/s²)", color="Direction") gg <- gg + legend_theme print(gg) # library(plotly) # ggplotly(gg) #ggplot(subset(move, Key %in% c("11", "12"))) + # geom_line(aes(x=Time, y=Value, color=factor(Key))) + # factor makes color discrete # legend_theme # extract accelerations xdata <- subset(move, Key == 11, select=c("Time","Value")) ydata <- subset(move, Key == 12, select=c("Time","Value")) zdata <- subset(move, Key == 13, select=c("Time","Value")) # substract mean (eliminate acceleration due to gravity) xdata$Acceleration = xdata$Value - mean(xdata$Value) ydata$Acceleration = ydata$Value - mean(ydata$Value) zdata$Acceleration = zdata$Value - mean(zdata$Value) # integrate twice to optain position data xdata$Position = c(0, cumsum(cumsum(xdata$Acceleration[-1]*diff(xdata$Time))*diff(xdata$Time))) ydata$Position = c(0, cumsum(cumsum(ydata$Acceleration[-1]*diff(ydata$Time))*diff(ydata$Time))) zdata$Position = c(0, cumsum(cumsum(zdata$Acceleration[-1]*diff(zdata$Time))*diff(zdata$Time))) # Visualize results ggplot() + geom_point(aes(x=xdata$Time,y=xdata$Position)) + labs(title = "X-Movement", x = "Time", y = "X") + theme ggplot() + geom_point(aes(x=ydata$Time,y=ydata$Position)) + labs(title = "Y-Movement", x = "Time", y = "Y") + theme ggplot() + geom_point(aes(x=zdata$Time,y=zdata$Position)) + labs(title = "Z-Movement", z = "Time", y = "Z") + theme ggplot() + geom_point(aes(x=xdata$Position,y=ydata$Position)) + labs(title = "X-Y-Movement", x = "X", y = "Y") + theme ggplot() + geom_point(aes(x=ydata$Position,y=zdata$Position)) + labs(title = "X-Z-Movement", x = "X", y = "Z") + theme ggplot() + geom_point(aes(x=xdata$Position ,y=zdata$Position)) + labs(title = "Y-Z-Movement", x = "Y", y = "Z") + theme library(rgl) plot3d(xdata$Position,ydata$Position,zdata$Position, box = TRUE, axes = TRUE) library(plotly) df <- data.frame(x = xdata$Position, y = ydata$Position, z = zdata$Position) plot_ly(df, x = ~x, y = ~y, z = ~z)