I am trying to visualize certain data in an HTML file using R. Environment:
- R: version 4.2.2
- R Studio: version 2022.07.2+576
- libraries: tidyverse, plotly
I have 100 CSV-Files named 00_Trace, ... , 99_Trace
. Each file has three columns (time, Cylinder_0, Cylinder_1) and 500 data points plus one 1 title lines. The lines represent the trace of two positions over time.
Importing the CSV-Files leads to data of the following form:
time filename Cylinder_0 Cylinder_1
...
979.971 00_Trace_Retract.csv 1.9 9.0
981.967 00_Trace_Retract.csv 2.0 5.0
984.062 00_Trace_Retract.csv 1.7 9.0
985.964 00_Trace_Retract.csv 1.7 1.0
987.995 00_Trace_Retract.csv 1.3 1.0
989.978 00_Trace_Retract.csv 1.6 9.0
991.973 00_Trace_Retract.csv 1.6 1.0
994.091 00_Trace_Retract.csv 1.6 1.0
995.960 00_Trace_Retract.csv 1.7 1.1
997.986 00_Trace_Retract.csv 1.7 1.0
...
I can mutate the values to numeric, pivot_longer the data set to one value and additional column for Position_A or Position_B.
What I want is basically to have a legend in order to choose which file(s) (00_Trace ... 99_Trace) shall be shown; always two traces. The color of the line should be based on Position_A (blue) and Position_B (grey).
I tried:
data <- readAllCsvData("data") %>%
rename(time = `time..s.`) %>%
select(-X) %>%
mutate(time = as.numeric(time)) %>%
mutate(Cylinder_0 = as.numeric(Cylinder_0)) %>%
mutate(Cylinder_1 = as.numeric(Cylinder_1)) %>%
mutate(Dev_new = Cylinder_0 - Cylinder_1) %>%
mutate(Deviation = as.numeric(Deviation)) %>%
pivot_longer(cols = c(Cylinder_0, Cylinder_1), names_to = "Cylinder")
gg <- data %>%
filter(grepl(".*Advance.*", filename)) %>%
ggplot() +
geom_line(aes(x = time, y = value, group = filename, color = Cylinder))
ggplotly(gg)
But I only get the following. Colors are basically alright, just need to change the actually used color. But I can only select Cylinder_0 or Cylinder_1, not for example 50_Trace.
Can this be solved in an easy way?