Home > Software design >  Highlighting the part of a line graph that has the highest slope in R
Highlighting the part of a line graph that has the highest slope in R

Time:03-03

My dataset contains 2 variables Y and X. Y was measured every 1.0 seconds.

My Data:

 dput(Dataexample)
structure(list(X = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 
14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 
30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 
46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 
62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 
78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 
94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 
108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 
121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 
134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 
147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 
160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 
173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 
186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 
199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 
212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 
225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 
238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 
251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 
264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 
277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 
290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302, 
303, 304, 305, 306), Y = c(71756.2344, 71745.85, 70882.42, 71025.61, 
70539.02, 70602.3047, 70811.87, 70514.125, 69998.63, 70531.76, 
70424.9141, 70663.51, 70075.375, 69731.0859, 70029.74, 70519.31, 
69858.63, 69987.23, 70080.56, 69970.63, 69829.6, 69872.12, 69775.68, 
69679.24, 69814.05, 69639.84, 69645.02, 69344.35, 69430.41, 70078.49, 
69239.65, 69734.1953, 69736.27, 69549.63, 69506.0859, 69108, 
69669.91, 69516.45, 69490.54, 69609.77, 69314.29, 69454.25, 69590.07, 
69721.76, 69525.79, 69736.27, 69303.92, 69171.23, 69294.59, 69430.41, 
69457.36, 69462.54, 69144.27, 69590.07, 69446.99, 70083.67, 69358.87, 
69800.56, 69680.28, 69332.95, 69723.83, 69942.63, 69772.56, 69969.59, 
69808.86, 70043.23, 70208.13, 70077.45, 69856.56, 70423.875, 
69490.54, 69984.12, 70175.98, 70192.58, 70279.7, 70480.93, 70594, 
70792.16, 70234.06, 70165.61, 70249.62, 70564.95, 70403.13, 70444.625, 
70426.99, 69907.375, 70327.4141, 70686.3359, 70473.67, 71031.83, 
70864.78, 70710.1953, 70691.52, 70703.97, 70826.39, 70708.12, 
70595.04, 70946.75, 71319.27, 70977.875, 70475.74, 70612.68, 
70680.11, 70527.61, 70461.22, 70877.2344, 70631.35, 70723.68, 
70677, 70433.21, 70306.6641, 71246.63, 70375.125, 70416.62, 70150.0547, 
70733.0156, 70583.63, 70866.86, 70580.5156, 70433.21, 70377.2, 
70114.79, 70347.12, 70613.71, 70576.37, 70599.19, 70407.28, 70581.5547, 
70650.02, 71122.11, 70909.4, 70694.63, 71076.45, 70650.02, 71133.52, 
70810.83, 71240.41, 70630.31, 71144.94, 71493.63, 71117.95, 71374.28, 
71143.9, 70805.64, 71349.375, 71208.2344, 71322.39, 71727.1641, 
71060.88, 71546.56, 71569.4, 70984.1, 72032.37, 71573.55, 71787.375, 
71469.76, 71398.15, 71683.57, 71709.52, 71637.9, 71556.9453, 
71870.4141, 71612.99, 71953.47, 71515.43, 71315.125, 72007.4453, 
72021.9844, 71549.68, 72001.22, 71359.75, 71775.95, 72327.23, 
71949.31, 71844.47, 71857.96, 72128.9141, 72147.6, 71501.94, 
72268.05, 72104, 72217.1641, 72253.51, 72198.48, 72908.78, 72084.27, 
72653.29, 72431.06, 72858.92, 72512.0547, 72632.5156, 72700.02, 
72335.53, 72713.52, 73065.62, 72818.42, 73004.3359, 72458.06, 
73436.48, 73231.82, 73002.26, 73313.89, 73213.125, 72980.4453, 
72948.25, 73106.13, 72931.625, 73409.47, 73057.31, 73141.4453, 
73218.32, 73216.24, 73273.375, 73701.42, 73486.35, 72574.37, 
73229.74, 73576.74, 73195.46, 73697.2656, 73115.48, 73065.62, 
73062.5, 73111.32, 73988.23, 73619.3359, 73874.95, 73683.76, 
73674.41, 73550.7656, 74166.9844, 73875.99, 74013.17, 74092.16, 
73872.875, 74015.25, 73984.07, 73911.33, 73606.87, 74082.8, 73866.64, 
74550.53, 74271.95, 73980.95, 74502.71, 74901.92, 74753.25, 74310.4141, 
75178.51, 74748.05, 74756.37, 75194.1, 74797.95, 75531.0547, 
75549.77, 75293.94, 75378.17, 75457.21, 75676.67, 76087.56, 76141.6641, 
76008.5, 76241.55, 76585.96, 76091.73, 76880.4844, 76898.18, 
77005.38, 77080.32, 77548.78, 77337.4453, 77000.18, 77448.8359, 
76997.0547, 77314.54, 77919.47, 77185.46, 78127.75, 77464.45, 
78349.59, 77824.71, 77465.49, 77818.46, 78140.25, 78547.51, 77850.74, 
78236.06, 78341.2656, 78104.8359, 78464.17, 77888.23, 78392.3, 
78686.0547, 78149.625, 78623.5547, 78672.5156, 78810.03, 78498.55, 
78652.72, 78717.31, 78831.91, 78882.96, 78715.23, 78499.5859, 
78892.3359, 78372.51)), row.names = c(NA, -306L), class = c("tbl_df", 
"tbl", "data.frame"))

I have used ggplot to plot the data and used a loop to calculate the average slope within a moving 60-second-window for the entire duration of the dataset to find the 60 consecutive seconds where the slope is greatest.

Code:

library(readr)
library(ggplot2)

Dataexample<- read_csv("HF-6.csv", skip = 3) 
Dataexample<- head(Dataexample, -1)

Dataexample$X <- as.numeric(Dataexample$X)

df <- data.frame(Dataexample)
 

ggplot(data=df, aes(x=X, y=Y, group=1))  
  geom_line()


slopes <- rep(NA, nrow(Dataexample)-59)
for( i in 1:length(slopes)){
  slopes[i] <- lm(Y ~ X, data=Dataexample[i:(i 59), ])$coefficients[2]
}

print(slopes)

which.max(slopes)
max(slopes)

Plot

My questions is how can I then take the results of my loop that show the consecutive 60 seconds where the slope is highest and change the color of the line in the plot during those 60 seconds to highlight where slope is greatest.

CodePudding user response:

This should work:

maxslope_ind <- which.max(slope)
Dataexample$highlight <- ifelse(Dataexample$X %in% maxslope_ind:(maxslope_ind 59), 1, 0)
library(ggplot2)
ggplot(data=Dataexample, aes(x=X, y=Y, group=1))  
  geom_line(aes(colour=as.factor(highlight)), show.legend=FALSE)   
  scale_colour_manual(values=c("black", "red"))

enter image description here

  • Related