neither-nor - 10 months ago 46
R Question

# Align x axis in frequency plot of a list of dates

I'm working with a set of data, each containing a list of 3 dates related to when one of three sequential actions is taken:

``````action.1 <- sample(seq(as.Date('2010-01-01'), as.Date('2010-02-01'), by="day"), 200, replace=TRUE)
action.2 <- action.1 + sample(seq(0,10,by=1), 200, replace=TRUE)
action.3 <- action.2 + sample(seq(0,40,by=1), 200, replace=TRUE)
``````

In order to visualize how the durations between actions depend on when
`action.1`
is taken -- for example, does higher frequency of
`action.1`
prolong the time for taking
`action.2`
?, I plot

``````rankn <- rank(action.1, ties.method = "first")

attach(mtcars)
layout(matrix(c(1,2), 2, 1, byrow = TRUE))

d1 = min(action.1, action.2, action.3)
d2 = max(action.1, action.2, action.3)

plot(table(action.1))

plot(action.1, rankn, col="red3", xlim=c(d1, d2))
points(action.2, rankn, col='green3')
points(action.3, rankn, col='blue3')
``````

and obtain this figure:

However, I cannot figure out how to align the two figures so that the upper bound of the x axis in the frequency plot coincides with the rightmost red dot. I tried setting
`plot(table(action.1)), xlim=c(d1,d2))`
, but it just made the top figure disappear. Is there any way around this?

Find the common part between lists, which should be `max(min(list_1), min(list_2),...)` to `min(max(list_1), max(list_2),...)`.

So the problem is with your `d2`: `d2 = 2010-03-15`, instead you should assign it with:

``````d2 <- min(max(action.1), max(action.2), max(action.3))
``````

Then, `d2 = 2010-02-01`, also with `d1` I think, assign it with `d1 <- max(min(action.1), min(action.2), min(action.3))`. If this is your expected result(ignore the Chinese format):

EDIT: I prefer to use `par(mfrow = c(2,1))` to manage the plot layout. So:

``````  par(mfrow = c(2,1))

d1 <- max(min(action.1), min(action.2), min(action.3))
d2 <- min(max(action.1), max(action.2), max(action.3))

plot(table(action.1))

plot(action.1, rankn, col="red3", xlim=c(d1, d2))
points(action.2, rankn, col='green3')
points(action.3, rankn, col='blue3')
``````