按照这个问题,带有geom_tile和矩阵图的一个图块中的多个值,我希望
agreement_num
中的图块属性是对称的,即对于反向对组合,我想绘制相同的agreement_num
。我正在创建一个新问题,因为这需要找到对的反向组合,分配相应的 agreement_num
,然后为每个组合和 agreement_num
插入新行。例如。从哈萨克斯坦到吉尔吉斯斯坦有六行和六个独特的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), cut = c(3, 1, 1, 1,
1, 1, 1, 3, 3, 3, 3, 3, 4, 4, 1, 1), oneway = c(FALSE, TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, TRUE, FALSE), additional_value = list(NA, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA, NA, NA, NA, NA, NA, NA, c(51L, 176L, 58L, 224L, 133L,
135L), NA)), row.names = c(NA, -16L), class = "data.frame")
也许有一种更简单的方法可以做到这一点,很乐意获得建议。
如果您需要的只是反转名称,那么简单的行绑定怎么样?
library(dplyr)
rename(plot_data, from = to, to = from) |>
bind_rows(plot_data) |>
filter(to == "Kyrgyzstan")
# to from weight agreement_num com.x com.y cut oneway additional_value
# 1 Kyrgyzstan Kazakhstan 133799551 NA 2 2 1 TRUE 51, 176, 58, 224, 133, 135
# 2 Kyrgyzstan Uzbekistan 1102379005 NA 2 2 1 FALSE NA
# 3 Kyrgyzstan Kazakhstan 480160896152 51 2 2 1 TRUE NA
# 4 Kyrgyzstan Kazakhstan 480160896152 176 2 2 1 TRUE NA
# 5 Kyrgyzstan Kazakhstan 480160896152 58 2 2 1 TRUE NA
# 6 Kyrgyzstan Kazakhstan 480160896152 224 2 2 1 TRUE NA
# 7 Kyrgyzstan Kazakhstan 480160896152 133 2 2 1 TRUE NA
# 8 Kyrgyzstan Kazakhstan 480160896152 135 2 2 1 TRUE NA
(
filter
只是为了表明您想要的六行在那里)
transform(plot_data, to = from, from = to) |>
rbind(plot_data)