-1

I have data from a clinical trial indexed by Patient no. and treatment group in a data frame. The variables X1 through X7 correspond to observations taken on months 1 through 7. In general an improvement corresponds to a reduction in the measured parameter.

   Patient.No Group X1 X2 X3 X4 X5 X6 X7
1      PX-002            1 2  24  4  82  2  2  2
2      PX-009            5  77  6  36  33  1  2  0
3      PX-019            1  6  7  5  4  3  2  2
....
8      PX-001            54  7  7  5  5  3  2  1
9      PX-004            2  67  3  2  3  2  1  1
10     PX-013            3  5  24  4  2  1  1  2
11     PX-015            2  9  4  33  63  2  1  1

What's a good way to plot for say group-1 as separate time series one tracking for each patients evolution over 7 months. With months on the x axis?

This is an ugly prototype of what I mean:

enter image description here

I can do it manually but seems to clunky. Any better ways?

plot(unlist(data[data$Patient.No=="PX-002",c("X1","X2","X3","X4","X5","X6","X7")]),type="b")
lines(unlist(data[data$Patient.No=="PX-019",c("X1","X2","X3","X4","X5","X6","X7")]),type="b")

Or

grp1_data<-data[data$Treat..Group==1,]

plot.ts(y = unlist(grp1_data[grp1_data$Patient.No=="PX-002",c("X1","X2","X3","X4","X5","X6","X7")]),x = c(1,2,3,4,5,6,7)) 
curious_cat
  • 805
  • 2
  • 9
  • 24

1 Answers1

1

This is really easy in ggplot. By the way, you should post a reproducible example. I would have coded this for you if you had one. Hope this works.

library(dplyr) 
library(tidyr)
library(ggplot2)

df %>% 
  gather(Month, Value, -Patient.No, -Group) %>% 
  ggplot(aes(Month, Value, color = Group)) +
  geom_line(alpha = 0.75) + 
  ylab("Chosen Metric") +
  ggtitle("Patient Progress by Group")
Zafar
  • 1,897
  • 15
  • 33
  • Thanks Dan! I tried your sugesstion. But I get something like this. Any idea what's going wrong/ – curious_cat Jan 13 '17 at 05:06
  • Thanks Dan! I tried your suggestion. But I get something like this. Any idea what's going wrong? http://imgur.com/a/faEH8 – curious_cat Jan 13 '17 at 05:27
  • I couldn't say, it would really help if you edited your question to provide some data I could copy/paste in with a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Zafar Jan 13 '17 at 16:49
  • Thanks Dan. I will try and post one. – curious_cat Jan 14 '17 at 17:09