-1

i'm currently writing a code in R and I need a visualization of the data. Its about EDA data. In the rows I have the different samples (points of measurement) and every column is the data of one subject(V1, V2...). I tried many things and now did it with ggplot:

ggplot()+
  geom_line(data = eda_pos_z, aes(x=1:nrow(eda_pos_z), y=V1, col = "VP1")) + 
  geom_line(data = eda_pos_z, aes(x=1:nrow(eda_pos_z), y=V2, col = "VP2")) +   
  geom_line(data = eda_pos_z, aes(x=1:nrow(eda_pos_z), y=V3, col = "VP3")) +
  geom_line(data = eda_pos_z, aes(x=1:nrow(eda_pos_z), y=V4, col = "VP4")) +
  geom_line(data = eda_pos_z, aes(x=1:nrow(eda_pos_z), y=V5, col = "VP5")) +
  geom_line(data = eda_pos_z, aes(x=1:nrow(eda_pos_z), y=V6, col = "VP6")) +
  ylab('EDA') +
  xlab('samples')

With this I have all data lines in one graph but the problem is that I have 49 columns (so this is just an extract) and i guess there is a better/shorter way to achieve what i need. I just don't know how. Can anybody help me?

Thanks and sorry for possible spelling mistakes, Luisa :)

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214

1 Answers1

0

The following should work, and is the way ‘ggplot2’ is intended to be used:

eda_pos_z %>%
    mutate(x = row_number()) %>%
    pivot_longer(matches('^VP'), names_to = 'name', values_to = value) %>%
    ggplot() +
    geom_line(aes(x = x, y = value, color = name) + 
    ylab('EDA') +
    xlab('samples')

That is, the data from the VP* columns is first transformed into long form (akatidy data”), which puts the names into columns. Furthermore, aesthetics are usually based on variables, not on values (such as your manually created x-values).

(The above uses the packages ‘dplyr’ for mutate, and ‘tidyr’ for pivot_longer … other ways of achieving the same result exists, including in base R, but these two packages are worth learning/using anyway.)

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214