简体   繁体   English

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

[英]Connect all points using lines and write text above it using R

I'm trying to connect every point in my array with all other points in this array using line segment and write some text slightly above this lines . 我试图使用线段将数组中的每个点与该数组中的所有其他点连接起来,并在此行的上方稍稍写一些文本 So, I want to achieve next: 因此,我要实现下一个目标:

在此处输入图片说明

I already tried to use segments() and lines() functions, but I don't know how can I do exactly what I described. 我已经尝试过使用segments()lines()函数,但是我不知道我该怎么做。

And as I said, now I have only array of coordinates and array of strings which I want to write. 就像我说的那样,现在我只有要编写的坐标数组和字符串数组。 How can I achieve this(It will be good if I will need to use only standard R libraries)? 我该如何实现(如果我只需要使用标准R库,那会很好)?

UPD: UPD:

dataset.csv: dataset.csv:

,A,B,C
A,0,1,2
B,1,0,3
C,2,3,0

script.r: 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]

Here's an example that uses combn to generate combinations of two points and then draw lines between them and to compute distances and write them in the middle too. 这是一个使用combn生成两个点的组合,然后在它们之间绘制lines并计算距离并将其写入中间的示例。

#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)

在此处输入图片说明

UPDATE UPDATE

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)

Trying to achieve this with graphics primitives (such as lines ) is bound to be a pain. 尝试使用图形基元(例如lines )来实现这一点注定很痛苦。

Use a dedicated library for graph plotting instead, eg ggraph . 请改用专用库进行图形绘制,例如ggraph The “Edges” vignette has an example with edge labels: “边缘”小插图有一个带有边缘标签的示例:

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)

在此处输入图片说明

The one drawback: ggraph doesn't allow you to explicitly set the node positions; 缺点之一:ggraph不允许您显式设置节点位置; however, you can manipulate them manually. 但是,您可以手动操作它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM