Reputation: 795
I have a plot that has date on the x-axis with some data points ("vertical lines") plotted. And then another plot that looks similar except the lines are plotted to different dates depending on when the "ActionType" occurred. The graph looks like how I want it to look except that I want to left align the x-axis based on the first Action Type that occurred. I want to preserve the space between the dates when the Action Types occurred.
So instead of having all the first Action Types plotted to the date that they occurred, they would be plotted to the same point (an absolute scale, I guess?) but then the second event would be plotted reflecting the spacing from the first event and so forth and so forth. Another way I'm thinking about it is shifting the x-axis of the lower plots to line up the first events in all the plots.
I've tried starting the x-axis at the same point for all the graphs, but this doesn't work because the lines are mapped to the dates that they occurred. I've also tried facet_grid
with scales= "free"
but because each x-axis needs to be different I don't think this will work either. I would love to be able to visualize 15 unique email addresses, but I'm starting with 3 for now.
My data looks like this and it's called email_2:
email ActionType date
1 xxxx Email 12/5/14
2 xxxx Email 11/26/14
3 xxxx won 9/28/14
4 xxxx Email 11/26/14
5 xxxx Email 11/6/14
6 xxxx Email 11/26/14
7 xxxx demo 12/1/14
8 xxxx Demo 2/9/15
9 xxxx Email 9/15/14
10 xxxx trial 9/9/14
11 xxxx Meeting 9/24/14
12 xxxx Email 12/1/14
13 xxxx Email 11/6/14
14 xxxx Email 9/15/14
15 xxxx Email 9/15/14
16 xxxx Email 11/26/14
17 yyyy Email 9/4/14
18 yyyy trial 8/14/14
19 yyyy Email 8/25/14
20 yyyy Email 8/21/14
21 yyyy won 9/3/14
22 yyyy Email 9/11/14
23 yyyy Email 8/21/14
24 yyyy Email 11/6/14
25 yyyy Email 8/21/14
26 yyyy Email 11/6/14
27 yyyy Email 11/6/14
28 yyyy Email 8/21/14
29 yyyy Email 11/6/14
30 yyyy Email 8/21/14
31 yyyy Email 8/29/14
32 yyyy Email 8/21/14
33 zzzz Email 11/6/14
34 zzzz Email 11/6/14
35 zzzz Email 9/18/14
36 zzzz trial 9/17/14
37 zzzz Email 9/18/14
38 zzzz Email 12/5/14
39 zzzz Email 11/6/14
40 zzzz Email 11/6/14
41 zzzz Email 12/15/14
42 zzzz Email 9/19/14
43 zzzz Email 11/6/14
44 zzzz Email 12/3/14
45 zzzz Email 12/3/14
46 zzzz Email 12/15/14
47 zzzz Email 10/1/14
48 zzzz Email 12/5/14
49 zzzz won 10/2/14
50 zzzz Email 9/18/14
My code to produce the visual looks like this:
email_3 <- subset(email_2, email == 'xxxx')
p1 <- ggplot(data=email_3,aes(date,ActionType)) +
scale_x_date(limits=as.Date(c("2014-08-14","2015-02-10"))) +
scale_y_continuous(email_3$email, breaks=NULL, limits=c(0,1)) +
geom_vline(data=email_3, mapping=aes(xintercept = as.numeric(date),
color=ActionType),size = 2,show_guide=F) +
theme(axis.title.x=element_blank()) +
theme(axis.title.y=element_text(angle=360))
email_4 <- subset(email_2, email == 'yyyy')
p2 <- ggplot(data=email_4,aes(date,ActionType)) +
scale_x_date(limits=as.Date(c("2014-08-14", "2014-11-07"))) +
scale_y_continuous(email_4$email, breaks=NULL, limits=c(0,1)) +
geom_vline(data=email_4, mapping=aes(xintercept = as.numeric(date),
color=ActionType),size=2,show_guide=F) +
theme(axis.title.x=element_blank()) +
theme(axis.title.y=element_text(angle=360))
email_5 <- subset(email_2, email == 'zzzz')
p3 <- ggplot(data=email_5,aes(date,ActionType)) +
scale_x_date(limits=as.Date(c("2014-08-14", "2014-12-16"))) +
scale_y_continuous(email_5$email, breaks=NULL, limits=c(0,1)) +
geom_vline(data=email_5, mapping=aes(xintercept = as.numeric(date),
color=ActionType),size=2,show_guide=T) +
theme(legend.position="bottom") +
theme(axis.title.x=element_blank()) +
theme(axis.title.y=element_text(angle=360))
grid.arrange(p1,p2,p3, ncol=1)
Upvotes: 1
Views: 375
Reputation: 13280
Why not use facetting?
email_2 <- read.table(header = TRUE, text = ' email ActionType date
1 xxxx Email 12/5/14
2 xxxx Email 11/26/14
3 xxxx won 9/28/14
4 xxxx Email 11/26/14
5 xxxx Email 11/6/14
6 xxxx Email 11/26/14
7 xxxx demo 12/1/14
8 xxxx Demo 2/9/15
9 xxxx Email 9/15/14
10 xxxx trial 9/9/14
11 xxxx Meeting 9/24/14
12 xxxx Email 12/1/14
13 xxxx Email 11/6/14
14 xxxx Email 9/15/14
15 xxxx Email 9/15/14
16 xxxx Email 11/26/14
17 yyyy Email 9/4/14
18 yyyy trial 8/14/14
19 yyyy Email 8/25/14
20 yyyy Email 8/21/14
21 yyyy won 9/3/14
22 yyyy Email 9/11/14
23 yyyy Email 8/21/14
24 yyyy Email 11/6/14
25 yyyy Email 8/21/14
26 yyyy Email 11/6/14
27 yyyy Email 11/6/14
28 yyyy Email 8/21/14
29 yyyy Email 11/6/14
30 yyyy Email 8/21/14
31 yyyy Email 8/29/14
32 yyyy Email 8/21/14
33 zzzz Email 11/6/14
34 zzzz Email 11/6/14
35 zzzz Email 9/18/14
36 zzzz trial 9/17/14
37 zzzz Email 9/18/14
38 zzzz Email 12/5/14
39 zzzz Email 11/6/14
40 zzzz Email 11/6/14
41 zzzz Email 12/15/14
42 zzzz Email 9/19/14
43 zzzz Email 11/6/14
44 zzzz Email 12/3/14
45 zzzz Email 12/3/14
46 zzzz Email 12/15/14
47 zzzz Email 10/1/14
48 zzzz Email 12/5/14
49 zzzz won 10/2/14
50 zzzz Email 9/18/14')
email_2$date <- as.Date(email_2$date, format = '%m/%d/%y')
str(email2)
require(ggplot2)
ggplot(data = email_2, aes(date, ActionType)) +
scale_x_date(limits = as.Date(c("2014-08-14", "2014-12-16"))) +
scale_y_continuous(email_2$email, breaks = NULL, limits = c(0, 1)) +
geom_vline(data = email_2, mapping=aes(xintercept = as.numeric(date),
color = ActionType), size = 2,show_guide = T) +
theme(legend.position="bottom") +
theme(axis.title.x=element_blank()) +
theme(axis.title.y=element_text(angle=360)) +
facet_grid(email~.)
grid.arrange(p1,p2,p3, ncol=1)
Upvotes: 1