Jeśli chcesz to zrobić w ggplot2
, możesz użyć do tego geom_segment
. Zanim jednak będziesz mógł wykonać taki wykres, musisz stworzyć ramkę danych, która łączy każdą obserwację z innymi obserwacjami. Można go zbliżyć się następująco:
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())
co daje:
Używane dane:
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)
Alternatywnie, można również dodać wiersz - w locie bez robi to wcześniej:
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())
„Chcę, że linia jest rysowana między wszystkich współrzędnych” to znaczy, że chcesz każdy punkt należy podłączyć do każdego innego punktu? Nie jestem pewien, czy ma go ggplot, ale może być tutaj lepszy pakiet do rysowania wykresów. – Marsenau
Tak, każdy pojedynczy punkt musi być połączony z każdym innym punktem – Jelmer
wypróbuj pakiet 'igraph' i utwórz kompletny wykres, a następnie przypisz wierzchołek współrzędne w oparciu o ramkę danych –