Home > front end >  Scale x axis for time sequence to 0-100%
Scale x axis for time sequence to 0-100%

Time:11-05

I have several measurements, with each measurement taking a different amount of time (5 seconds, 8 seconds etc.)

I want to plot these measurements grouped as an e.g. line graph but scale the time (x-axis) to 0-100%, so that these measurements are evenly spread.

Here is an example:

library(tidyverse)
measurement <- c("first", "second", "third", "fourth")
group <- c("A", "B", "A", "B")
time_1 <- c("1", "2", "1", NA)
time_2 <- c(NA, "3", NA, "4")
time_3 <- c("10", "15", "17", "19")
time_4 <- c("5", NA, "8", NA)
time_5 <- c("1", "2", "1", "1")
time_6 <- c("3", NA, NA, NA)
time_7 <- c("4", NA, NA, NA)
time_8 <- c("2", NA, NA, NA)

test_plot <- data.frame(measurement, group, time_1,
                        time_2, time_3, time_4, time_5, time_6, time_7,
                        time_8)


gather_testplot <- gather(test_plot, key="time", value="value",
                          "time_1", "time_2", "time_3", "time_4", "time_5",
                          "time_6", "time_7", "time_8")

gather_testplot_nona <- gather_testplot %>%
  drop_na(value)


gather_testplot$time <- as.numeric(as.character(gather_testplot$time))

ggplot(gather_testplot_nona, aes(x=time, y=value, color=group,
                                 group=interaction(group)))  
  geom_line()

enter image description here

  • Related