在图中的所有坐标之间画线 [英] Draw lines between all the coordinates in a plot
问题描述
data< - data.frame(x = c(5,1,3) ,2,5,7,12),y = c(5,7,6,1,3,5,6))
我可以用ggplot函数绘制这些坐标,并在这些坐标之间画一条线:
ggplot (data,aes(x,y))+ geom_point(size = 3)+ geom_line()
到目前为止,没有问题。但是,通过坐标而不是一条线,我想要在所有坐标之间画一条线。在所有坐标之间创建一种蜘蛛网。这可能在 ggplot2
包中? 对此使用 geom_segment
。但在你做出这样的阴谋之前,你必须创建一个数据框,将每个观察结果与其他观察结果连接起来。你可以按如下方式处理它:
library(ggplot2)
library(dplyr)
library(tidyr )
dat%>%
complete(nesting(x,y),id)%>%#创建组合
select(id,xend = x,yend = y)% >%#将新变量重命名为终点
left_join(dat,。,by ='id')%>%#与原始数据帧连接
filter(!(x == xend & y == yend))%>%#删除与起始点相同的端点
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 < - 数据.frame(x = c(5,1,3,2,5,7,12),y = c(5,7,6,1,3,5,6))
dat $ id< - 1:nrow(dat)
另外,你也可以事先添加row-id而不用预先执行:
dat%>%
mutate( id = row_number())%>%#添加一行id
complete(nesting(x,y),id)%>%#创建组合
select(id,xend = x, yend = y)%>%#将新变量重命名为终点
left_join(dat%>%mutate(id = row_number()),。,
by ='id')% >%#加入原始数据框(也包含一个添加的行ID)
filter(!(x == xend& y == yend))%>%#删除与开始点相同的端点
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())
I have the following dataframe:
data <- data.frame(x = c(5,1,3,2,5,7,12), y = c(5,7,6,1,3,5,6))
I can plot these coordinates with the ggplot function and draw a line between these coordinates:
ggplot(data, aes(x, y)) + geom_point(size = 3) + geom_line()
So far, no problems. But instead of a single line though the coordinates, I want that a line is drawn between all the coordinates. Creating a sort of spider web between all the coordinates. Is this possible in the ggplot2
package?
If you want to do this in ggplot2
, then you could use geom_segment
for this. But before you can make such a plot, you have to create a dataframe which connencts each observation to the other observations. You could approach it as follows:
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())
which gives:
Used data:
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)
Alternatively, you can also add the row-id on the fly without doing it beforehand:
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())
这篇关于在图中的所有坐标之间画线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!