gpt4 book ai didi

r - 连接散点图中的所有点(可能的组合)

转载 作者:行者123 更新时间:2023-12-04 17:53:56 25 4
gpt4 key购买 nike

这是小数据集:

myd <- data.frame(PC1 = rnorm(5, 5, 2), 
PC2 = rnorm (5, 5, 3), label = c("A", "B", "C", "D", "E"))
plot(myd$PC1, myd$PC2)
text( myd$PC1-0.1, myd$PC2, lab = myd$label)

我想将直线(欧几里得)距离之间的所有可能组合连接起来,以生成这样的图形(最好在基本图形或 ggplot2 中)

enter image description here

最佳答案

这是基本情节解决方案:

plot(myd$PC1, myd$PC2)
apply(combn(seq_len(nrow(myd)), 2), 2,
function(x) lines(myd[x, ]$PC1, myd[x, ]$PC2))

enter image description here

这是ggplot2解决方案:
ps <- data.frame(t(apply(combn(seq_len(nrow(myd)), 2), 2, 
function(x) c(myd[x, ]$PC1, myd[x, ]$PC2))))
qplot(myd$PC1, myd$PC2) +
geom_segment(data = ps, mapping = aes(x = X1, xend = X2, y = X3,yend = X4))

enter image description here

关于r - 连接散点图中的所有点(可能的组合),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11472563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com