gpt4 book ai didi

r - 绘制表格和连接它们的箭头

转载 作者:行者123 更新时间:2023-12-05 01:58:06 26 4
gpt4 key购买 nike

我想知道是否可以使用 ggplot2 绘制类似下面示例的内容。如果没有,能否请您指导我使用合适的工具在 R 中完成此操作?谢谢!

enter image description here

最佳答案

简短回答:是的,当然有可能。这在一定程度上取决于您的用例是什么,取决于您想要放入其中的自动化程度。棘手的一点显然是箭头位,在尝试自动化方法时它变得有点复杂,所以我会把它留给你 - 只是展示在 ggplot2 中获得它的一种原则方法。

library(tidyverse)
ls_tbl <- replicate(3, data.frame(label = paste0("Field", 5:1)), simplify = FALSE)
## make the data frames for tables and rects
ls_df <- lapply(seq_along(ls_tbl), function(i){
df <- rbind(ls_tbl[[i]], paste0("Table", i))
df$y <- seq_len(nrow(df))
df$x <- i
df_rect <- data.frame(xmin = i-.2, xmax = i+.2, ymin = min(df$y)-.5, ymax = max(df$y)+.5)
y_seg <- (df$y[length(df$y)] + df$y[length(df$y)-1])/2
df_seg <- data.frame(x = i-.2, xend = i+.2, y =y_seg, yend = y_seg)
setNames(list(df, df_rect, df_seg), c("text", "rect", "seg"))
})
## draw arrows, this can be automated based on the position of fields and tables
## that's not suuuuper straight forward, so I won't do that here
arrow_df <- data.frame(x = c(1, 3), xend = rep(2, 2), y = c(5, 5), yend = c(2, 4)) %>%
mutate(x = ifelse(xend > x, x + .2, x - .2),
xend = ifelse(xend > x, xend- .2, xend + .2))

## now plotting fun
ggplot() +
geom_rect(data = bind_rows(map(ls_df, "rect")), color = "black", fill = NA,
aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax)) +
geom_segment(data = bind_rows(map(ls_df, "seg")), aes(x, y, xend = xend, yend = yend))+
geom_text(data = bind_rows(map(ls_df, "text")), aes(x, y, label = label)) +
geom_segment(data = arrow_df, arrow = arrow(type = "closed"),
aes(x, y, xend = xend, yend = yend)) +
theme_void()

reprex package 创建于 2021-12-24 (v2.0.1)

关于r - 绘制表格和连接它们的箭头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68711765/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com