[英]How to connect points of two dataframes to each other using ggplot in R?
[英]How to connect points of two different plots to each other using ggplot in R?
我有兩個數據幀df1
和df2
如下:
> 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
值。 實際上,這是我想要的圖表:
我為了獲得上面的圖而編寫的代碼是:
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"))
但結果是:
我使用gtable對你的問題有一個輕微的hacky解決方案。 首先,我們將制作類似於您制作它們的圖形。 我改變的是在ggplot()
調用中指定公共映射。 接下來,在geom_segment()
我將yend分別設置為-Inf
和Inf
。
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)
在我手中,產生了以下情節:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.