You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

实现geom_tile矩阵图反向国家对agreement_num对称与行添加

问题:实现矩阵图中反向国家对的agreement_num对称显示

需要让geom_tile矩阵图中tile的agreement_num属性保持对称:即反向国家对需显示相同的agreement_num。具体要求为:找到原数据中已存在的反向国家对,为其分配对应的agreement_num并插入新行。

例如,哈萨克斯坦到吉尔吉斯斯坦的6行数据对应6个唯一agreement_num,需为吉尔吉斯斯坦到哈萨克斯坦的行添加如下数据:

from        to          weight          agreement_num
Kyrgyzstan  Kazakhstan  1.337996e+08    51
Kyrgyzstan  Kazakhstan  1.337996e+08    176
Kyrgyzstan  Kazakhstan  1.337996e+08    58
Kyrgyzstan  Kazakhstan  1.337996e+08    224
Kyrgyzstan  Kazakhstan  1.337996e+08    133
Kyrgyzstan  Kazakhstan  1.337996e+08    135

现有代码(未完成最后步骤)

已完成部分逻辑,但无法实现插入新行并为每行分配agreement_num的最后步骤:

# Create empty vectors to store values
oneway <- logical(nrow(plot_data))
additional_value <- list()

# Iterate over each row of the data frame
for (i in 1:nrow(plot_data)) {
  # Find indices of reverse pairs
  reverse_indices <- which(plot_data$from == plot_data$to[i] & plot_data$to == plot_data$from[i] & seq_len(nrow(plot_data)) != i)
  
  # Assign the oneway value
  oneway[i] <- length(reverse_indices) > 0
  
  # Assign additional value based on reverse combination
  if (oneway[i]) {
    # Add the additional values from the "agreement_num" column of the reverse pairs
    additional_value[[i]] <- unique(plot_data$agreement_num[reverse_indices])
  } else {
    # Assign NA if oneway is FALSE
    additional_value[[i]] <- NA
  }
}

# Add the vectors as new columns to the data frame
plot_data$oneway <- oneway
plot_data$additional_value <- additional_value

可复现数据

plot_data <- structure(list(from = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L), levels = c("Afghanistan", 
"Kazakhstan", "Kyrgyzstan", "Tajikistan", "Turkmenistan", "Uzbekistan"
), class = "factor"), to = structure(c(1L, 3L, 3L, 3L, 3L, 3L, 
3L, 4L, 4L, 4L, 5L, 5L, 6L, 6L, 2L, 6L), levels = c("Afghanistan", 
"Kazakhstan", "Kyrgyzstan", "Tajikistan", "Turkmenistan", "Uzbekistan"
), class = "factor"), weight = c(1291072130433.34, 480160896152.234, 
480160896152.234, 480160896152.234, 480160896152.234, 480160896152.234, 
480160896152.234, 3474907531417.02, 3474907531417.02, 3474907531417.02, 
867103764128.709, 867103764128.709, 7791981051421.92, 7791981051421.92, 
133799551.098735, 1102379004.66647), agreement_num = c(NA, 51L, 
176L, 58L, 224L, 133L, 135L, 58L, 51L, 224L, 51L, 224L, 51L, 
224L, NA, NA), com.x = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L), com.y = c(2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L)), class = "data.frame", row.names = c(NA, 
-16L))

期望输出示例

from        to          weight          agreement_num
Kazakhstan  Afghanistan 1.291072e+12    NA
Kazakhstan  Kyrgyzstan  4.801609e+1     51
Kazakhstan  Kyrgyzstan  4.801609e+1     176
Kazakhstan  Kyrgyzstan  4.801609e+1     58
Kazakhstan  Kyrgyzstan  4.801609e+1     224
Kazakhstan  Kyrgyzstan  4.801609e+1     133
Kazakhstan  Kyrgyzstan  4.801609e+1     135
[other rows]
Kyrgyzstan  Kazakhstan  1.337996e+08    51
Kyrgyzstan  Kazakhstan  1.337996e+08    176
Kyrgyzstan  Kazakhstan  1.337996e+08    58
Kyrgyzstan  Kazakhstan  1.337996e+08    224
Kyrgyzstan  Kazakhstan  1.337996e+08    133
Kyrgyzstan  Kazakhstan  1.337996e+08    135

不期望输出示例

from        to          weight          agreement_num
Kazakhstan  Afghanistan 1.291072e+12    NA
Kazakhstan  Kyrgyzstan  4.801609e+1     51
Kazakhstan  Kyrgyzstan  4.801609e+1     176
Kazakhstan  Kyrgyzstan  4.801609e+1     58
Kazakhstan  Kyrgyzstan  4.801609e+1     224
Kazakhstan  Kyrgyzstan  4.801609e+1     133
Kazakhstan  Kyrgyzstan  4.801609e+1     135
Afghanistan Kazakhstan  1.291072e+12    NA #(不希望出现此行,因原数据无该反向对)
[other rows]
Kyrgyzstan  Kazakhstan  1.337996e+08    51  
Kyrgyzstan  Kazakhstan  1.337996e+08    176
Kyrgyzstan  Kazakhstan  1.337996e+08    58
Kyrgyzstan  Kazakhstan  1.337996e+08    224
Kyrgyzstan  Kazakhstan  1.337996e+08    133
Kyrgyzstan  Kazakhstan  1.337996e+08    135

绘图代码

# Set agreement_num as factor

plot_data$agreement_num <- as.factor(plot_data$agreement_num)

# Allow multiple tiles

plot_data <- plot_data |>
  mutate(
    x = as.numeric(from),
    y = as.numeric(to),
    ymin = y - .5, ymax = y + .5
  ) |>
  mutate(
    n = n(),
    xmin = x + scales::rescale(row_number(),
                               from = c(1, unique(n) + 1),
                               to = .5 * c(-1, 1)
    ),
    xmax = x + scales::rescale(row_number() + 1,
                               from = c(1, unique(n) + 1),
                               to = .5 * c(-1, 1)
    ),
    .by = c(from, to)
  )


# Create color palette

par(mar=c(0,0,1,0))

coul <- brewer.pal(9, "Set3") 

# Plot
ggplot(plot_data, aes(x = from, y = to, fill = agreement_num)) +
  geom_rect(
    aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax)
  ) +
  scale_fill_manual(values = coul) +
  theme_bw() +
  scale_x_discrete(drop = FALSE) +
  scale_y_discrete(drop = FALSE) +
  labs(title = "Community 2") +
  theme(plot.title = element_text(size=17),
    axis.text.x = element_text(
      size = 12, angle = 270,
      hjust = 0, vjust = 0
    ),
    axis.text.y = element_text(size = 12),
    axis.title.x = element_text(size = 15),
    axis.title.y = element_text(size = 15),
    aspect.ratio = 1
  )

需求

寻求更简洁的实现方案,或完成现有代码中插入新行并分配agreement_num的最后步骤。


内容的提问来源于stack exchange,提问作者MoonS

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 16:27:34