如何在R中使用ggplot将两个不同图的点彼此连接?

时间:2019-05-10 16:41:15

标签: r ggplot2

我有两个数据帧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的每个值连接到df2的对应值。其实,这是我想要的图表:

output

我为尝试获得以上图表而编写的代码是:

library(grid)
library(dplyr)

plot1 <- df1 %>%
  select(dateTime, value) %>%
  na.omit() %>%
  ggplot() +
  geom_point(data = df1, aes(dateTime, value)) +
  geom_line(data = df1, aes(x = dateTime, y = value), color = 'green') +
  geom_segment(data = setNames(cbind(df1, df2), c("x1", "y1", "x2", "y2")),
                aes(x = x1, y = y1, xend = x2, yend = y2), 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) %>%
  na.omit() %>%
  ggplot() +
  geom_point(data = df2, aes(dateTime, value)) + 
  geom_line(data = df2, aes(x = dateTime, y = value), color = 'red') +
  geom_segment(data = setNames(cbind(df1, df2), c("x1", "y1", "x2", "y2")),
                aes(x = x1, y = y1, xend = x2, yend = y2), linetype = "dashed") +
  xlab("dateTime") +
  theme(axis.text=element_text(size = 14), axis.title=element_text(size = 14))

grid.newpage()
grid.draw(rbind(ggplotGrob(plot1), ggplotGrob(plot2), size = "last"))

但是结果是:

output2

1 个答案:

答案 0 :(得分:2)

对于您使用gtable的问题,我有一个有点棘手的解决方案。首先,我们将使绘图与您的制作相似。我所做的更改是在ggplot()调用中指定通用映射。接下来,在geom_segment()中,将图的段中的日元分别设置为-InfInf

plot1 <- df1 %>%
  select(dateTime, value) %>%
  na.omit() %>%
  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) %>%
  na.omit() %>%
  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))

这时,我们将组合的grob转换为gtable:

library(gtable)
gt <- rbind(ggplotGrob(plot1), ggplotGrob(plot2), size = "last")

从该gtable中,我们查找在绘图面板之间放置一个额外图形对象所需的值。我建议您不要盲目地相信您的面板杂讯位于gt$grobs[[6]]中,而这些细分市场不在gt$grobs[[6]]$children[[5]]中。亲自查看数据结构,并确认这些元素在gtable中的位置。

# 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

现在,我们将制作一个新的grob,它模仿段的x位置,但跨越整个视口。我们将其添加到gtable中。

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