0

I have a dataframe (20 variables (diagnostic codes) with 43 measurements each)--in which the row number corresponds to the day of the measurement (sort of like time series data) picture of table.

I want to plot the measurement over time (ie from day 1 to day 43) for each variable.

I tried first creating a new variable called days so that I could plot it as the x-axis

tarp_d<-tarp_t %>%   mutate(days=1:43)

But now I'm having a hard time figuring out how to plot all of the 20 variables in one plot, without having to add each variable individually like:

ggplot(tarp_d, aes(x=days))+geom_point(aes(y=TP21A))+geom_point(aes(x=days, y=TP26A))

Does anyone know how to automate this? Or know of a way in which I can directly use the row number as the x-axis without having to create a new variable? I also thought of

And, for the record this is the structure:

> str(tarp_s) 'data.frame': 43 obs. of  21 variables:  
$ TP21A: num  1.42 1.36 1.3 1.25 1.19 ...  
$ TP21B: num  1.39 1.33 1.28 1.22 1.17 ...  
$ TP24A: num  1.88 1.82 1.75 1.69 1.62 ...  
$ TP24B: num  1.67 1.6 1.54 1.47 1.41 ...  
$ TP25A: num  1.4 1.4 1.4 1.4 1.4 ...  
$ TP25B: num  1.33 1.3 1.26 1.23 1.2 ...  
$ TP26A: num  1.51 1.49 1.48 1.46 1.44 ...  
$ TP26B: num  1.46 1.43 1.4 1.36 1.33 ...  
$ TP27A: num  1.86 1.85 1.84 1.84 1.83 ...  
$ TP27B: num  1.53 1.48 1.44 1.39 1.34 ...  
$ TP27C: num  1.42 1.39 1.35 1.31 1.27 ...  
$ TP28A: num  1.66 1.65 1.64 1.63 1.62 ...  
$ TP28B: num  1.38 1.35 1.31 1.28 1.24 ...  
$ TP29A: num  1.47 1.47 1.47 1.47 1.47 ...  
$ TP29B: num  1.46 1.42 1.37 1.33 1.28 ...  
$ TP30A: num  1.48 1.48 1.48 1.48 1.48 ...  
$ TP30B: num  1.25 1.25 1.25 1.25 1.25 ...  
$ TP30C: num  1.03 1.03 1.03 1.03 1.03 ...  
$ TP70Z: num  1.31 1.31 1.31 1.31 1.31 ...  
$ TP96Z: num  NA NA NA NA NA NA NA NA NA NA ...  
$ days : num  1 2 3 4 5 6 7 8 9 10 ...
jpsmith
  • 11,023
  • 5
  • 15
  • 36
A_K_2018
  • 9
  • 2
  • 1
    Welcome to SO! It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data. If you want to post your data type `dput(NAME_OF_DATASET)` into the console and copy the output starting with `structure(....` into your post. If your dataset has a lot of observations you could do e.g. `dput(head(NAME_OF_DATASET, 10))` for the first ten rows of data. – stefan Apr 03 '22 at 13:37
  • ... this said: The trick is to convert your data to long format using e.g. `tidyr::pivot_longer`, i.e. try `tarp_d_long <- tarp_d %>% pivot_longer(-days)` – stefan Apr 03 '22 at 13:38
  • Thank you!! And sorry I'm terrible at R not to mention stack exchange! – A_K_2018 Apr 03 '22 at 14:50

1 Answers1

1

Are you looking for such a solution?

Explanation: First bring your dataframe in long format with pivot_longer from tidyr package (many reasons why!). then you can plot using ggplot2 with geom_point and geom_line after defining color and group.

library(tidyverse)

df %>% 
  mutate(day = row_number()) %>% 
  pivot_longer(
    -day
  ) %>% 
  ggplot(aes(x = day, y = value, color = name, group=name)) +
  geom_point()+
  geom_line()

enter image description here data:

df <- structure(list(TP21A = c(1.415, 1.36, 1.304, 1.249, 1.193, 1.138, 
1.082, 1.08, 1.077, 1.074, 1.072, 1.069, 1.066, 1.063, 1.061, 
1.058, 1.055), TP21B = c(1.388, 1.333, 1.277, 1.222, 1.166, 1.111, 
1.056, 1.052, 1.048, 1.044, 1.04, 1.036, 1.032, 1.028, 1.024, 
1.02, 1.016), TP24A = c(1.882, 1.817, 1.753, 1.688, 1.624, 1.56, 
1.495, 1.431, 1.366, 1.302, 1.299, 1.295, 1.292, 1.288, 1.285, 
1.281, 1.278), TP24B = c(1.666, 1.602, 1.538, 1.474, 1.41, 1.346, 
1.282, 1.218, 1.216, 1.213, 1.21, 1.207, 1.204, 1.201, 1.198, 
1.195, 1.192), TP25A = c(1.396, 1.396, 1.396, 1.396, 1.396, 1.396, 
1.396, 1.396, 1.396, 1.396, 1.396, 1.396, 1.396, 1.396, 1.396, 
1.396, 1.396), TP25B = c(1.329, 1.297, 1.264, 1.232, 1.2, 1.167, 
1.135, 1.102, 1.07, 1.038, 1.036, 1.035, 1.034, 1.033, 1.032, 
1.031, 1.03), TP26A = c(1.509, 1.493, 1.477, 1.46, 1.444, 1.427, 
1.411, 1.395, 1.378, 1.362, 1.345, 1.329, 1.312, 1.296, 1.28, 
1.263, 1.247), TP26B = c(1.464, 1.431, 1.398, 1.365, 1.332, 1.299, 
1.266, 1.233, 1.2, 1.167, 1.134, 1.101, 1.068, 1.067, 1.065, 
1.064, 1.063)), class = "data.frame", row.names = c(NA, -17L))
TarJae
  • 72,363
  • 6
  • 19
  • 66