ggplot:按数据点对齐图

时间:2015-04-10 15:45:57

标签: r ggplot2 facet gridextra

我有一个在x轴上有日期的绘图,其中绘制了一些数据点(“垂直线”)。然后根据“ActionType”的发生时间,将另一个看起来相似但除了线条的图表绘制到不同的日期。该图看起来像我希望它看起来除了我想要根据发生的第一个Action Type左对齐x轴。我想保留动作类型发生的日期之间的空间。

因此,不是将所有第一个动作类型绘制到它们出现的日期,而是将它们绘制到相同的点(绝对比例,我猜?)但是然后将绘制第二个事件来反映与第一次事件等等。我正在考虑的另一种方法是移动较低图的x轴以排列所有图中的第一个事件。

我已尝试在所有图形的同一点开始x轴,但这不起作用,因为线条被映射到它们发生的日期。我也尝试facet_gridscales= "free",但因为每个x轴需要不同,我认为这也不会有用。我希望能够可视化15个独特的电子邮件地址,但我现在开始使用3个。

我的数据看起来像这样,它叫做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

我生成视觉效果的代码如下:

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)

1 个答案:

答案 0 :(得分:1)

为什么不使用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) enter image description here