使用线连接所有点并使用R在其上面写入文本

时间:2017-06-28 16:04:16

标签: r plot

我正在尝试使用线段将数组中的每个点与此数组中的所有其他点连接,并在此行上稍微写一些文字。所以,我想实现下一步:

enter image description here

我已经尝试使用lines(),A,B,C A,0,1,2 B,1,0,3 C,2,3,0 函数,但我不知道如何才能完全按照我的描述进行操作。

正如我所说,现在我只有一些坐标和字符串数组,我想写。 我怎样才能做到这一点(如果我只需要使用标准的R库,那将会很好)?

UPD:

dataset.csv:

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]

script.r:

.right-of-blue {
  box-shadow: 8px 0 transparent;
}

.outline, .background {
  border-color: transparent;
  background-color: transparent;
}

// Clicking the button toggles this class on the wrapper div
.colors-enabled {
  .should-animate {
    transition:
    background-color .5s,
    box-shadow .5s,
    border-color .5s;
  }

  .outline {
    border-width: 1px 2px;
    margin: 0 -2px;
    background: none !important;
    border-style: solid;
  }

  .background {
    border-width: 1px;
    margin: 0 -1px;
    border-style: solid;
  }

  .red {
    border-color: $red;
    background-color: $red;
  }

  .blue {
    border-color: $blue;
    background-color: $blue;
  }

  .right-of-blue {
    box-shadow: 8px 0 $blue;

    // Bump the outline/background of the next segment over to make room for the box shadow
    & + span.outline {
      margin-left: 0;

      // Bump the text inside back to keep it from moving
      & > span > span {
        margin-left: -2px;
      }
    }
  }
}

2 个答案:

答案 0 :(得分:3)

这是一个使用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)

enter image description here

<强>更新

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)

答案 1 :(得分:1)

尝试使用图形基元(例如lines)来实现这一点必将是一种痛苦。

使用专用库进行图形绘制,例如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)

enter image description here

一个缺点:ggraph不允许你明确设置节点位置;但是,可以手动操作它们。