如果你想在ggplot2
中这样做,那么你可以使用geom_segment
这个。但在你做出这样的情节之前,你必须创建一个数据框,将每个观察结果与其他观察结果连接起来。
library(ggplot2)
library(dplyr)
library(tidyr)
dat %>%
complete(nesting(x,y), id) %>% # create the combinations
select(id, xend=x, yend=y) %>% # rename the new variables as end-points
left_join(dat, ., by = 'id') %>% # join with the original dataframe
filter(!(x==xend & y==yend)) %>% # remove the endpoints that are the same as the start points
ggplot(., aes(x, y)) +
geom_segment(aes(x = x, y = y, xend = xend, yend = yend)) +
geom_label(aes(x = x, y = y, label = id, color = factor(id)), show.legend = FALSE) +
theme_minimal(base_size = 14) +
theme(axis.title = element_blank())
这给:可以按如下方式处理它
使用的数据:
dat <- data.frame(x = c(5,1,3,2,5,7,12), y = c(5,7,6,1,3,5,6))
dat$id <- 1:nrow(dat)
另外,您还可以添加该行 - 没有这样做事先:
dat %>%
mutate(id = row_number()) %>% # add a row id
complete(nesting(x,y), id) %>% # create the combinations
select(id, xend=x, yend=y) %>% # rename the new variables as end-points
left_join(dat %>% mutate(id = row_number()), .,
by = 'id') %>% # join with the original dataframe (also with an added row id)
filter(!(x==xend & y==yend)) %>% # remove the endpoints that are the same as the start points
ggplot(., aes(x, y)) +
geom_segment(aes(x = x, y = y, xend = xend, yend = yend)) +
geom_label(aes(x = x, y = y, label = id, color = factor(id)), show.legend = FALSE) +
theme_minimal(base_size = 14) +
theme(axis.title = element_blank())
“我想要一条线的所有坐标之间绘制”这是否意味着你希望每一个单点连接到所有其他点? 不确定ggplot是否有它,但有些图形绘制软件包在这里可能更好用 – Marsenau
是的,每一个点都必须连接到其他每个点 – Jelmer
尝试使用'igraph'软件包并制作完整的图形,然后指定顶点根据你的数据帧的坐标 –