在R中使用ggplot将移位图与另一个图的点连接起来

时间:2019-06-11 11:04:04

标签: r ggplot2 lag

我有两个数据帧df1df2,如下所示:

> df1
  dateTime value
1        1     6
2        2     2
3        3     3
4        4     1

> df2
  dateTime value
1        1     3
2        2     8
3        3     4
4        4     5

我只想在一张图中绘制这些数据框,将它们分成具有相同x轴的两个不同的图,将df1右移1,然后连接{{1}的每个值}到df1的对应值。这是我的代码:

df2

这是结果,但是实际上我要删除它的另外一行,并且#Shift df1 by 1 to the right df1$value <- lag(df1$value, 1) plot1 <- df1 %>% select(dateTime, value) %>% ggplot(aes(dateTime, value)) + geom_point() + geom_line(color = "green") + geom_segment(aes(xend = dateTime, yend = -Inf), linetype = "dashed") + theme(axis.text=element_text(size = 14), axis.title=element_text(size = 14), axis.title.x=element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank()) plot2 <- df2 %>% select(dateTime, value) %>% ggplot(aes(dateTime, value)) + geom_point() + geom_line(color = "red") + geom_segment(aes(xend = dateTime, yend = Inf), linetype = "dashed") + xlab("dateTime") + theme(axis.text=element_text(size = 14), axis.title=element_text(size = 14)) gt <- rbind(ggplotGrob(plot1), ggplotGrob(plot2), size = "last") # Panel positioning is_panel <- which(gt$layout$name == "panel") panel_x <- unique(gt$layout$l[is_panel]) panel_y <- gt$layout$t[is_panel] # Coordinates and graphical parameters for segments x_coords <- gt$grobs[[is_panel[1]]]$children[[5]]$x0 gpar <- gt$grobs[[is_panel[1]]]$children[[5]]$gp linkgrob <- segmentsGrob(x0 = x_coords, y0 = 0, x1 = x_coords, y1 = 1, gp = gpar) gt <- gtable_add_grob(gt, linkgrob, t = panel_y[1] + 1, l = panel_x, b = panel_y[2] - 1) grid.newpage() grid.draw(gt) 的最后一个值也没有意义,我也想显示最后一点:

enter image description here

1 个答案:

答案 0 :(得分:1)

滞后

我想lag可能是错误的函数:

lag(1:3)
# [1] NA 1 2 

如果我对您的理解正确,那么您想转移数据,这取决于您的真实数据,但是对于这个虚拟示例,类似

df1 <- df1 %>%
   mutate(dateTime = dateTime + 1)

应该可以解决问题。

您需要对基本图进行一些调整:

plot1 <- df1 %>%
  select(dateTime, value) %>%
  ## create a temp variable to which we can map the line type to
  mutate(lty = ifelse(dateTime == max(dateTime), "none", "dashed")) %>% 
  ggplot(aes(dateTime, value)) +
  geom_point() +
  geom_line(color = "green") +
  ## map the linetype to this variable
  geom_segment(aes(xend = dateTime, yend = -Inf, linetype = lty)) +
  ## use a manual scale to map the variable to dashed and blank linetype
  scale_linetype_manual(values = c(dashed = "dashed", none = "blank"),
                        guide = "none") + 
  ## add xlim to align scales properly in both plots
  xlim(c(1, 5)) +
  theme(axis.text=element_text(size = 14), axis.title=element_text(size = 14),
        axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank())

plot2 <- df2 %>%
  select(dateTime, value) %>%
  mutate(lty = ifelse(dateTime == min(dateTime), "none", "dashed")) %>%
  ggplot(aes(dateTime, value)) +
  geom_point() +
  geom_line(color = "red") +
  geom_segment(aes(xend = dateTime, yend = Inf, linetype = lty)) +
  scale_linetype_manual(values = c(dashed = "dashed", none = "blank"),
                        guide = "none") + 
  xlab("dateTime") +
  xlim(c(1, 5)) +
  theme(axis.text=element_text(size = 14), axis.title=element_text(size = 14))

这给你这个情节:

Linechart