0

I am trying to make a timeline using ggplot2 library with the following code:

library(ggplot2)

data <- read.csv("Data.csv", header = TRUE, sep = ",")
data$Date <- as.Date(data$Date)

xnow <- Sys.Date()

Names <- unique(data$Name)
i <- 0
plot<-ggplot(data,aes(x=Date,y=Name))+geom_point(aes(colour = factor(Event)))

for(i in 1:length(Names)){
  personname <- Names[i]
  xstart <- data[which(data$Event=="start" & data$Name== personname),]$Date
  xfinal <- data[which(data$Event=="end of engagement" & data$Name== personname),]$Date
  plot<-plot+geom_point(aes(x = Sys.Date(), y = personname), colour = "green", size = 3)
  plot<-plot+geom_segment(aes(x=xstart ,xend=xnow,y = personname, yend = personname))
  plot<-plot+geom_segment(aes(x=xnow ,xend=xfinal,y = personname, yend = personname), linetype = 2)
}

After the first iteration i am getting expected line segment for the name "devanshu". During second iteration the previous line segment for name "devanshu " disappears as soon as first command of iteration is executed. Data:

Name        Date        Event
devanshu    2/2/2016    start
devanshu    3/2/2016    first self assessment
devanshu    6/3/2016    first 360 assessment
devanshu    12/7/2016   end of engagement
Priya       4/2/2016    start
Priya       6/3/2016    first self assessment
Priya       10/4/2016   first 360 assessment
Priya       2/7/2017    end of engagement

The problem is in code or in logic?

Axeman
  • 32,068
  • 8
  • 81
  • 94
  • 2
    It's such a shame that SO doesn't have OCR of all the linked and pasted images that newbies post. – IRTFM Jul 13 '16 at 21:11
  • Please don't tempt us with data as images. We can't copy and paste that, and nobody is going to type that in! [Please have a look here.](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Axeman Jul 13 '16 at 21:24
  • I can't figure out what you are trying to do here. But in general, you want to reorganize your data so you can make a `ggplot` call with a single `geom_point` and no loops, and plot everything at once. – Axeman Jul 13 '16 at 21:33
  • I am trying to plot different events across the dates for different persons in one graph. using the geom_point out of loop is working fine, but `plot<-plot+geom_segment(aes(x=data[which(data$Event=="start" & data$Name== Name),]$Date ,xend=xnow,y = Name, yend = Name))` is giving error: `Error: Aesthetics must be either length 1 or the same as the data (8): x, y, xend, yend` – devanshu singhal Jul 13 '16 at 21:52

1 Answers1

0

It's often easiest to reshape a dataset rather than using a loop when working with ggplot2. But see here for other ideas.

In this case, you need a second dataset for adding the segments that is in a wide format so you can set the end points of geom_segment using start and end of engagement as variables.

Here I make this dataset using spread from tidyr.

library(tidyr)
widedat = spread(data, Event, Date)
widedat

      Name end of engagement first 360 assessment first self assessment      start
1 devanshu        2016-12-07           2016-06-03            2016-03-02 2016-02-02
2    Priya        2017-02-07           2016-10-04            2016-06-03 2016-04-02

Now you can add the geom_segment layers using this new dataset.

ggplot(data, aes(x = Date, y = Name)) +
    geom_point(aes(colour = factor(Event))) +
    geom_point(aes(x = Sys.Date()), colour = "green", size = 3) +
    geom_segment(data = widedat, aes(x = start, xend = Sys.Date(), 
                               yend = Name)) +
    geom_segment(data = widedat, aes(x = Sys.Date(), xend = `end of engagement`, 
                               yend = Name), linetype = 2)

enter image description here

Community
  • 1
  • 1
aosmith
  • 34,856
  • 9
  • 84
  • 118