-2

I want to create a time-line graph (with ggplot function?) in R for 276 cases separately but displayed in one figure. They are exposed to different treatments for different times and can have multiple events. I want to mark exposure to different treatments by using colors and mark events during these treatment strategies in this graph (e.g. with dots).

my data look like this

Does someone know (partly) how to do this?

nrussell
  • 18,382
  • 4
  • 47
  • 60

1 Answers1

1

Your data is not in what is called 'Tidy format'. You first want to do some data wrangling to get your data from 'broad' to 'long' format before you get to plotting your data. Your different treatments should be in a single column (called treatments) instead of multiple columns. I suggest you look into the dplyr and tidyr packages (see this cheatsheet).

To put your treatments into rows rather than columns you could use the gather function from the tidyr package. Once this is done you could create a ggplot similar to:

ggplot(your_data, aes(x = time_of_event, y = value, colour = treatment)) +
  geom_point()
Jeroen Boeye
  • 580
  • 4
  • 18