-1

I have the following data set

set.seed(1)
startdate <- as.Date('2000-01-01')  
enddate <- as.Date('2000-01-10')   

Data <- data.frame(id = rep((1:1000),10), 
                   group = rep(c("0","1"), 25),
                   IV = sample(1:100),
                   DV = sample(c("1", "0"), 10, replace = TRUE),
                   date = as.Date(
                     sample(as.numeric(start_date):
                              as.numeric(end_date), 1000,
                            replace = T), origin = '1970-01-01'))

I want to get two density plots for multiple groups (here, group = 1 and group = 0) and a vertical line on a defined point. How do I do this?

  • the response helped too, couldn't figure out the combination of the plots and the line and didn't know if the combination required a different syntax. Turns out it didn't, thanks @Ivo –  Mar 28 '18 at 09:59

1 Answers1

0

To get the density plots and the line, do (description see in code comments)

Data$date_f <- as.factor(Data$date) # date as factor
Data$date_i <- as.integer(Data$date_f) # date as int
Data$date <- Data$date_i[!is.na(Data$date_i)] # excl missing

# date by group
date_1 <- Data$date_i[Data$group == "1"] # date group 1
date_2 <- Data$date_i[Data$group == "0"] # date group 2

# exclude missing
date_1  <- date_1[!is.na(date_1)]
date_2 <- date_2[!is.na(date_2)]
#View(date_i)

#plot
plot(density(date_1), xaxt='n', xlab = 'Date', lwd = 2.5, ylab = 'Density', main = 'Density and Line', las=1, col = "black", lty = 1) # line and labels
lines(density(date_2), col = 'blue', lwd = 2.5, lty = 1) # other line, repeat for each group
abline(v= 8, col='black', lwd = 1.5, lty = 1) # vertical line
tx=seq(min(date_1), max(date_1), by = 2) #labels
lb=levels(Data$date_f)[tx] #insert labels
axis(side = 1,at=tx,labels=lb, las=0.2) #insert axis
Ivo
  • 3,890
  • 5
  • 22
  • 53