Wenn Sie dies in ggplot2
tun möchten, können Sie dafür geom_segment
verwenden. Bevor Sie jedoch einen solchen Plot erstellen können, müssen Sie einen Datenrahmen erstellen, der jede Beobachtung mit den anderen Beobachtungen verbindet. Man könnte es Ansatz wie folgt:
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())
die gibt:
Gebrauchte Daten:
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)
Alternativ können Sie auch die Zeile hinzufügen -im Vorbeigehen ohne tut es vorher:
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())
„Ich möchte, dass eine Linie zwischen allen Koordinaten gezogen wird,“ bedeutet das, dass Sie jeden einzelnen Punkt zu jedem anderen Punkt verbunden werden möchten? Nicht sicher, ob ggplot es hat, aber einige Graph Plotten Paket könnte hier besser sein – Marsenau
Ja, jeder einzelne Punkt muss mit jedem anderen Punkt verbunden werden – Jelmer
versuchen Sie das 'igraph' Paket und machen Sie einen vollständigen Graphen, dann zuweisen Vertex Koordinaten auf der Grundlage Ihrer Datenrahmen –