Reputation: 2307
I have a data set that looks like this:
The sample data can be build by the codes:
ID<-c("1","1","1","1","2","2","2","3","3","3","4","4","4","4")
Days<-c("-5","1","18","30","1","8","16","1","8","6","-6","1","7","15")
Event<-c("","","P","","","","N","","","C","","","P","N")
Test_1<-c("82","95","67","99","58","57","68","25","87","68","77","87","98","55")
Test_2<-c("66","85","96","35","27","69","55","27","39","24","55","99","89","58")
Sample.data <- data.frame(ID, Days, Event, Test_1, Test_2)
I would like to make a plot that look like this but have no clue how to make it. It is too complex for me to even make a sample plot in excel. so I hand draw one to show what I want to achieve. Sorry for the ugly graph. Anyone can help and guide me on this? Thanks.
Upvotes: 0
Views: 295
Reputation: 39613
This could be a good point to start. Please next time do not include all values in variables between quotes. Only characters must be quoted in R
. I inspected your data and found a way to obtain a similar curves to what you want. I do not understand the variable Event
as this has empty values. But using a long format data you can be able to create a similar plot using score values in test variables. Here the code using functions from tidyverse
packages ggplot2
, dplyr
and tidyr
:
library(tidyverse)
#Data
ID<-c(1,1,1,1,2,2,2,3,3,3,4,4,4,4)
Days<-c(-5,1,18,30,1,8,16,1,8,6,-6,1,7,15)
Test_1<-c(82,95,67,99,58,57,68,25,87,68,77,87,98,55)
Test_2<-c(66,85,96,35,27,69,55,27,39,24,55,99,89,58)
Event<-c("","","P","","","","N","","","C","","","P","N")
#Dataframe
Sample.data <- data.frame(ID, Days, Event, Test_1, Test_2,stringsAsFactors = F)
#Reshape
Sample.data %>% pivot_longer(-c(1:3)) %>%
mutate(Event=ifelse(Event=="",NA,Event)) %>%
ggplot(aes(x=Days,y=value,group=name,color=name,label=Event))+
geom_line()+
geom_text(position = position_dodge(0.9),show.legend = F)+
facet_wrap(.~ID)+
theme_bw()
Output:
Upvotes: 2