0

I am plotting data over time using the following dataset:

dt1 <- data.table(
sender=c("boy", "girl", "girl", "boy", "girl", "boy"), 
type = c("run", "walk", "run", "run", "run", "walk"), 
time=c(as.POSIXct("2014-02-19 03:24:00"), as.POSIXct("2014-02-19 03:29:00"), as.POSIXct("2014-02-20 03:30:00"), as.POSIXct("2014-02-23 03:34:00"), as.POSIXct("2014-02-25 08:24:00"), as.POSIXct("2014-02-25 09:45:00")), 
dayRelative = c(0,0,1,4,6,6)) 

The following ggplot command works fine:

ggplot(dt1, aes(x=time, y=sender, colour=type)) +  
geom_point(size=2, position=position_jitter(width=0.2, height=0.2)) + 
scale_y_discrete(limit = c("boy", "girl"), labels= c("Boy", "Girl"))

Resulting Chart

What I like to achieve is that instead of having the date labels on the x-axis, the data from the dayRelative-column should be used. That means for the dots the time-column should be used, however, the x-Axis should use the data from the dayRelative column the respective places. As a result Feb 19 should be replaced by 0, Feb 20 by 1, ...

2
  • You can just replace time with dayRelative in the call to ggplot? What wil be the difference? Commented Aug 10, 2014 at 14:12
  • @BBrill The the dots are positioned by the less accurate variable dayRelative. time includes the exact time, dayRelative only the days passed since the beginning. I would like to have these days as labels on the x-Axis, still the dots should represent the exact times (like 9:00 on the first day). Commented Aug 10, 2014 at 14:20

1 Answer 1

1

You can use scale_x_datetime() and provide time as breaks= and dayRelative as labels=. Around the dt1$time used function trunc() to get only day without hours and minutes and then as.POSIXct to have POSIXct object to use it in scale_x_datetime().

ggplot(dt1, aes(x=time, y=sender, colour=type)) +  
      geom_point(size=2, position=position_jitter(width=0.2, height=0.2)) + 
      scale_y_discrete(limit = c("boy", "girl"), labels= c("Boy", "Girl"))+
      scale_x_datetime(breaks=as.POSIXct(trunc(dt1$time,"day")),labels=dt1$dayRelative)

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

This works in principle, but does not format nicely when there are many data points: for example it puts the label "6" as many times on the axis as it occurs. Is it possible to adjust this to include each label only once as Day 1, 2, 3, 4,... but not 0,0,0,0,1,2,2,2 (which is produced with my larger dataset)?
In your question you wrote that "x-axis should show 0,0,1,4,6,6...", so I answered as you asked. Will try to change the answer.
You are right, sorry for the confusion. I tried to adjust my question to reflect this.
Updated my answer with another solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.