Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

Problems editing plot

$
0
0

I have a plot function but is a bit static-y, so I am trying to clean it up with ggplot. This is a class assignment so, and I am told that there are 8 peaks to identify. They can kind of see them in the first version of the graph (linked below), but they need to be easily identifiable.

Here is the code for the plot:

plot(x=fatigue$Time[1:length(fatigue.norm)], y=fatigue.norm, type="l")

Which gives me this plot:

Then I am trying to use the following code to clean it up and identify peaks:

Tricep_flat <- flatten_dbl(as.list(fatigue.norm))

Tricep.peaks <- data.frame (findpeaks(Tricep_flat, npeaks=8, minpeakheight=.01, minpeakdistance=freq<-5))
colnames(Tricep.peaks) <- c ("Height", "Location", "Start", "End")
View(Tricep.peaks)

fatigue.norm <- data.frame("Time"=fatigue$Time[1:length(fatigue.norm)], 
                           "Tricep Signal"=fatigue$Triceps[1:length(fatigue.norm)])

ggplot(data=fatigue.norm, aes(x=Time))+
geom_line(aes(y=Tricep.Signal))+
geom_vline(xintercept=Tricep.peaks[1,2]/freq, linetype="dotted", color="blue")+
geom_vline(xintercept=Tricep.peaks[2,2]/freq, linetype="dotted", color="blue")+
geom_vline(xintercept=Tricep.peaks[3,2]/freq, linetype="dotted", color="blue")+
geom_vline(xintercept=Tricep.peaks[4,2]/freq, linetype="dotted", color="blue")+
geom_vline(xintercept=Tricep.peaks[5,2]/freq, linetype="dotted", color="blue")+
geom_vline(xintercept=Tricep.peaks[6,2]/freq, linetype="dotted", color="blue")+
geom_vline(xintercept=Tricep.peaks[7,2]/freq, linetype="dotted", color="blue")+
geom_vline(xintercept=Tricep.peaks[8,2]/freq, linetype="dotted", color="blue")

ggplot(data=Tricep.peaks, aes(x=Location, y=Height))+
geom_line()

This is the resulting graph:

This graph is completely different, and I thought that the code above would be altering the original plot.

Running packages are: signal, matlab, zoo, gglot2, pracma, purrr, dplyr

The whole Code:

DF.fatigue <- read.csv(file.choose(),header = FALSE, col.names = c("Time","Biceps","Triceps","Med-Lat Angle","Flex-Exten", "Shin","Calf"))
DF.MVC1 <-read.csv(file.choose(),header = FALSE, col.names = c("Time","Biceps","Triceps","Med-Lat Angle","Flex-Exten", "Shin","Calf"))
DF.MVC2 <- read.delim(file.choose(),header = FALSE, sep = "\t", col.names = c("Time","Biceps","Triceps","Med-Lat Angle","Flex-Exten", "Shin","Calf"))


install.packages(c("pracma", "purrr","signal","zoo","matlab", "ggplot2"))
library(pracma)
library(purrr)
library (dplyr)
library(signal)
library(zoo)
library(matlab)
library(ggplot2)                





## Remove Characters from the start of the data set leaving only EMG Signals
DF.fatigue.trim<- DF.fatigue[-(1:8),]
DF.MVC1.trim <- DF.MVC1[-(1:8),]
DF.MVC2.trim <- DF.MVC2 [-(1:8),]

#### Change integers to
fatigue<- data.frame("Time"=as.numeric(as.character(DF.fatigue.trim$Time)),
                     "Biceps"=as.numeric(unlist(as.character(DF.fatigue.trim$Biceps))),
                     "Triceps"=as.numeric(as.character(DF.fatigue.trim$Triceps)),
                     "Shin"=as.numeric(as.character(DF.fatigue.trim$Shin)),
                     "Flex_Exten"=as.numeric(as.character(DF.fatigue.trim$Flex.Exten)))

MVC1<-   data.frame("Time"=as.numeric(as.character(DF.MVC1.trim$Time)),
                     "Biceps"=as.numeric(as.character(DF.MVC1.trim$Biceps)),
                     "Triceps"=as.numeric(as.character(DF.MVC1.trim$Triceps)),
                     "Shin"=as.numeric(as.character(DF.MVC1.trim$Shin)))

MVC2<-   data.frame("Time"=as.numeric(as.character(DF.MVC2.trim$Time)),
                     "Biceps"=as.numeric(as.character(DF.MVC2.trim$Biceps)),
                     "Triceps"=as.numeric(as.character(DF.MVC2.trim$Triceps)),
                     "Shin"=as.numeric(as.character(DF.MVC2.trim$Shin)))


##### Find MVCs for Before and After ########
##### Step 1 Create Filter and Apply to Data Set

freq <- 2048
order <- 4
Low_hz <- 300
High_hz <- 400
Low_window <- 2*Low_hz/freq
High_window <- 2*High_hz/freq
Wband<- c(Low_window, High_window)
BW_HL <- butter(order, Wband)

MVC1.tricep<- signal::filter(BW_HL,DF.MVC1.trim$Triceps)
MVC2.tricep<- signal::filter(BW_HL,DF.MVC2.trim$Triceps)

###  Step 2  Rectify the data a.k.a. Absolute value of signals

MVC1.abs <- abs(MVC1.tricep)
MVC2.abs <- abs(MVC2.tricep)

#### Step 3  Low 10hz pass of Rectified data
lo_pass <-40
Wn <- 2*lo_pass/freq
bw <-butter(order,Wn)

MVC1.lf <- signal::filter(bw, MVC1.abs)
MVC2.lf <- signal::filter(bw, MVC2.abs)

#### Step 4 Take a one second Moving average of the data
#### HINT USE the rollmean function or for loop

MVC1.mean <- rollmean(x=MVC1.lf, k=freq)
MVC2.mean <- rollmean(x=MVC2.lf, k=freq)

### Step 5 Find the Maximum  for each Muscle Before and After

MVC.tricep <- c(max(MVC1.mean), max(MVC2.mean))


fatigue.tricep <- signal::filter(BW_HL,DF.fatigue.trim$Triceps)


fatigue.abs <- abs(fatigue.tricep)

# Low End 10hz Filter

fatigue.lf <- signal::filter(bw, fatigue.abs)

# Create a 1 second moving average

fatigue.mean <- rollmean(fatigue.lf, k=freq)

# Normalize the Vector to MVC

fatigue.norm <- fatigue.mean/max(MVC.tricep)







#####Plot

plot(x=fatigue$Time[1:length(fatigue.norm)], y=fatigue.norm, type="l")

#what can we say about the data? each maximum is steadily decreasing as fatigue sets in


########################################################################################################
## Other tools to help with analysis


### Create a Data frame with normalized data



Tricep_flat <- flatten_dbl(as.list(fatigue.norm))

Tricep.peaks <- data.frame (findpeaks(Tricep_flat, npeaks=8, minpeakheight=.01, minpeakdistance=freq<-5))
colnames(Tricep.peaks) <- c ("Height", "Location", "Start", "End")
View(Tricep.peaks)

fatigue.norm <- data.frame("Time"=fatigue$Time[1:length(fatigue.norm)], 
                           "Tricep Signal"=fatigue$Triceps[1:length(fatigue.norm)])

ggplot(data=fatigue.norm, aes(x=Time))+
geom_line(aes(y=Tricep.Signal))+
geom_vline(xintercept=Tricep.peaks[1,2]/freq, linetype="dotted", color="blue")+
geom_vline(xintercept=Tricep.peaks[2,2]/freq, linetype="dotted", color="blue")+
geom_vline(xintercept=Tricep.peaks[3,2]/freq, linetype="dotted", color="blue")+
geom_vline(xintercept=Tricep.peaks[4,2]/freq, linetype="dotted", color="blue")+
geom_vline(xintercept=Tricep.peaks[5,2]/freq, linetype="dotted", color="blue")+
geom_vline(xintercept=Tricep.peaks[6,2]/freq, linetype="dotted", color="blue")+
geom_vline(xintercept=Tricep.peaks[7,2]/freq, linetype="dotted", color="blue")+
geom_vline(xintercept=Tricep.peaks[8,2]/freq, linetype="dotted", color="blue")

ggplot(data=Tricep.peaks, aes(x=Location, y=Height))+
geom_line()

Tricep.peaks[1:2]

For DF.MVC1 file:///C:/Users/Owner/Desktop/MVC-%20Before.htm

DF.MVC2 file:///C:/Users/Owner/Desktop/Book1.htm

DF.fatigue file:///C:/Users/Owner/Desktop/Fatigue%20Array%20(2).htm


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>