使用线条连接所有点,并使用R在其上方书写文字

使用线条连接所有点,并使用R在其上方书写文字,r,plot,R,Plot,我试图使用线段将数组中的每个点与该数组中的所有其他点连接起来,并在这几行稍上方写一些文字。因此,我想实现下一个目标: 我已经尝试过使用segments()和lines()函数,但我不知道如何才能完全做到我所描述的 正如我说的,现在我只有坐标数组和字符串数组,我想写它们。 我怎样才能做到这一点(如果我只需要使用标准R库就好了) UPD: dataset.csv: ,A,B,C A,0,1,2 B,1,0,3 C,2,3,0 script.r: myDataset <- read.csv(

我试图使用线段将数组中的每个点与该数组中的所有其他点连接起来,并在这几行稍上方写一些文字。因此,我想实现下一个目标:

我已经尝试过使用
segments()
lines()
函数,但我不知道如何才能完全做到我所描述的

正如我说的,现在我只有坐标数组和字符串数组,我想写它们。 我怎样才能做到这一点(如果我只需要使用标准R库就好了)

UPD:

dataset.csv:

,A,B,C
A,0,1,2
B,1,0,3
C,2,3,0
script.r:

myDataset <- read.csv("dataset.csv")
row.names(myDataset) <- myDataset[, 1]
myDataset <- myDataset[, -1]
d <- dist(myDataset)
fit <- cmdscale(d,eig=TRUE, k=2)
x <- fit$points[,1]
y <- fit$points[,2]

<代码> MyDeaStudio

这里有一个例子,使用代码> COMBN <代码>生成两个点的组合,然后在它们之间绘制<代码>行< /代码>,并计算距离,并将它们写入中间。

#DATA
set.seed(42)
df = data.frame(x = rnorm(4),  y = rnorm(4))

#DRAW POINTS    
plot(df)

#DRAW LINES
combn(1:NROW(df), 2, function(x)
    lines(df[x,]), simplify = FALSE)

#WRITE TEXT
combn(1:NROW(df), 2, function(x)
    text(x = mean(df[x,1]),  #calculate center point x-value in the line
        y = mean(df[x,2]),  #calculate center point y-value in the line
        labels = round(dist(df[x,]), 2), #calculate distance to write
        srt = 180 * atan(diff(df[x, 2])/diff(df[x,1]))/pi, #calculate rotation angle of text
        pos = 3, #place text slightly above given x and y
        font = 2), #bold text
    simplify = FALSE)

更新

myDataset <- read.csv(strip.white = TRUE, stringsAsFactors = FALSE, header = TRUE, text = ",A,B,C
A,0,1,2
                      B,1,0,3
                      C,2,3,0")
row.names(myDataset) <- myDataset[, 1]
myDataset <- myDataset[, -1]
d <- dist(myDataset)
fit <- cmdscale(d,eig=TRUE, k=2)
x <- fit$points[,1]
y <- fit$points[,2]

df = data.frame(x, y)

#DRAW POINTS
plot(df, asp = 1)
text(x = df[,1], y = df[,2], labels = rownames(df), pos = 1)

#Create a list of combination of indices
temp = combn(1:NROW(df), 2, simplify = FALSE)

#DRAW LINES
sapply(temp, function(i) lines(df[i,]))

#WRITE TEXT
sapply(temp, function(x)
    text(x = mean(df[x,1]),  #calculate center point x-value in the line
        y = mean(df[x,2]),  #calculate center point y-value in the line
        labels = myDataset[cbind(which(row.names(myDataset) == row.names(df)[x[1]]),
                    which(colnames(myDataset) == row.names(df)[x[2]]))],
        srt = 180 * atan(diff(df[x, 2])/diff(df[x,1]))/pi, #calculate rotation angle of text
        pos = 3, #place text slightly above given x and y
        font = 2), #bold text
    simplify = FALSE)

<代码> MyDeaStudio

这里有一个例子,使用代码> COMBN <代码>生成两个点的组合,然后在它们之间绘制<代码>行< /代码>,并计算距离,并将它们写入中间。

#DATA
set.seed(42)
df = data.frame(x = rnorm(4),  y = rnorm(4))

#DRAW POINTS    
plot(df)

#DRAW LINES
combn(1:NROW(df), 2, function(x)
    lines(df[x,]), simplify = FALSE)

#WRITE TEXT
combn(1:NROW(df), 2, function(x)
    text(x = mean(df[x,1]),  #calculate center point x-value in the line
        y = mean(df[x,2]),  #calculate center point y-value in the line
        labels = round(dist(df[x,]), 2), #calculate distance to write
        srt = 180 * atan(diff(df[x, 2])/diff(df[x,1]))/pi, #calculate rotation angle of text
        pos = 3, #place text slightly above given x and y
        font = 2), #bold text
    simplify = FALSE)

更新

myDataset <- read.csv(strip.white = TRUE, stringsAsFactors = FALSE, header = TRUE, text = ",A,B,C
A,0,1,2
                      B,1,0,3
                      C,2,3,0")
row.names(myDataset) <- myDataset[, 1]
myDataset <- myDataset[, -1]
d <- dist(myDataset)
fit <- cmdscale(d,eig=TRUE, k=2)
x <- fit$points[,1]
y <- fit$points[,2]

df = data.frame(x, y)

#DRAW POINTS
plot(df, asp = 1)
text(x = df[,1], y = df[,2], labels = rownames(df), pos = 1)

#Create a list of combination of indices
temp = combn(1:NROW(df), 2, simplify = FALSE)

#DRAW LINES
sapply(temp, function(i) lines(df[i,]))

#WRITE TEXT
sapply(temp, function(x)
    text(x = mean(df[x,1]),  #calculate center point x-value in the line
        y = mean(df[x,2]),  #calculate center point y-value in the line
        labels = myDataset[cbind(which(row.names(myDataset) == row.names(df)[x[1]]),
                    which(colnames(myDataset) == row.names(df)[x[2]]))],
        srt = 180 * atan(diff(df[x, 2])/diff(df[x,1]))/pi, #calculate rotation angle of text
        pos = 3, #place text slightly above given x and y
        font = 2), #bold text
    simplify = FALSE)

myDataset试图用图形原语(如
)实现这一点肯定是一件痛苦的事

使用专用库进行图形打印,例如。“边缘”渐晕图有一个带有边缘标签的示例:

ggraph(simple, layout = 'graphopt') + 
    geom_edge_link(aes(label = type), 
                   angle_calc = 'along',
                   label_dodge = unit(2.5, 'mm'),
                   arrow = arrow(length = unit(4, 'mm')), 
                   end_cap = circle(3, 'mm')) + 
    geom_node_point(size = 5)


有一个缺点:ggraph不允许显式设置节点位置;但是,您可以手动操作它们。

尝试使用图形原语(如
)来实现这一点肯定会很痛苦

使用专用库进行图形打印,例如。“边缘”渐晕图有一个带有边缘标签的示例:

ggraph(simple, layout = 'graphopt') + 
    geom_edge_link(aes(label = type), 
                   angle_calc = 'along',
                   label_dodge = unit(2.5, 'mm'),
                   arrow = arrow(length = unit(4, 'mm')), 
                   end_cap = circle(3, 'mm')) + 
    geom_node_point(size = 5)


有一个缺点:ggraph不允许显式设置节点位置;但是,您可以手动操作它们。

如何存储字符串数组?它们与您希望绘制的线条有何关联?请使用
dput
并将结果复制到您的问题中,以便我们可以使用您的数据结构。如何存储字符串数组?它们与您希望绘制的线条有何关联?请使用
dput
并将结果复制到您的问题中,以便我们可以使用您的数据结构。对我来说,这几乎是完美的解决方案,但只有一个要求:您能否使用两个数字向量
x
y
(无
data.frame
)绘制坐标,并使用一个字符向量绘制文本标签?这对我真的很有帮助!我从
csv
文件中知道这一点,它有我应该写的距离(与绘图时不同),请检查更新的问题以了解更多细节。不幸的是,无法修复标签,
round(dist(df[x],2)
显示
dist(df)
,但我需要的正是
dataset.csv
文件中的标签,我尝试了一些东西,但仍然无法正确执行。这些是
dataset.csv
文件中的数字,例如,对于连接A点和B点的线,我需要用文本
1
,对于BC,我需要编写
3
等。谢谢!很好用!对我来说几乎是完美的解决方案,但只有一个要求:你能用两个数字向量
x
y
(无
data.frame
)绘制坐标,用一个字符向量绘制文本标签吗?这对我真的很有帮助!我从
csv
文件中知道这一点,它有我应该写的距离(与绘图时不同),请检查更新的问题以了解更多细节。不幸的是,无法修复标签,
round(dist(df[x],2)
显示
dist(df)
,但我需要的正是
dataset.csv
文件中的标签,我尝试了一些东西,但仍然无法正确执行。这些是
dataset.csv
文件中的数字,例如,对于连接A点和B点的线,我需要用文本
1
,对于BC,我需要编写
3
等。谢谢!很好用!您可以使用手动布局手动指定节点位置您可以使用手动布局手动指定节点位置