我正在分析我用字形包在R中构建的贩毒网络.它是定向网络数据集.我想在欧洲 map 上说明网络,所以我找到了如何获取国家首都的地理数据,并将其固定到网络对象上.我一直在try 让边缘在从起点到目的地的轨迹上有一个 colored颜色 渐变,以可视化边缘的方向,因为箭头相当笨重.我寻找一种方法来使这个值独立,因为我认为这是可能的.出于某种原因,我真的很难把这件事做完.我成功地使边缘的 colored颜色 依赖于纬度,但这并没有真正的帮助,因为它与方向无关.

这就是我到目前为止try 过的:


libs<- c("openrouteservice", "sf","tidyverse",
         "leaflet","maptiles","tidyterra","rnaturalearth","rnaturalearthdata",
         "CoordinateCleaner","igraph","ggraph","tidygraph")

# checking installed libs
installed_libs <- libs %in% rownames(
  installed.packages()
)

# installing missing libraries
if(any(installed_libs == F)){
  install.packages(
    libs[!installed_libs]
  )
}


# Activating libraries

invisible(
  lapply(libs,library,character.only = T)
)


world <- ne_countries(scale = "medium", returnclass = "sf")
Europe <- world[which(world$continent == "Europe"),]



node_list <-
  structure(
    list(
      country = c(
        "Austria",
        "Belgium",
        "Denmark",
        "Finland",
        "France",
        "Greece",
        "Germany",
        "Ireland",
        "Italy",
        "Luxembourg",
        "Netherlands",
        "Norway",
        "Portugal",
        "Sweden",
        "Spain",
        "Switzerland",
        "United Kingdom"
      ),
      drug = c(
        "Cocaine",
        "Cocaine",
        "Cocaine",
        "Cocaine",
        "Cocaine",
        "Cocaine",
        "Cocaine",
        "Cocaine",
        "Cocaine",
        "Cocaine",
        "Cocaine",
        "Cocaine",
        "Cocaine",
        "Cocaine",
        "Cocaine",
        "Cocaine",
        "Cocaine"
      ),
      level.of.sale = c(
        "Wholesale",
        "Wholesale",
        "Wholesale",
        "Wholesale",
        "Wholesale",
        "Wholesale",
        "Wholesale",
        "Wholesale",
        "Wholesale",
        "Wholesale",
        "Wholesale",
        "Wholesale",
        "Wholesale",
        "Wholesale",
        "Wholesale",
        "Wholesale",
        "Wholesale"
      ),
      wsprice = c(
        55753.26,
        38039.15,
        47449.58,
        97254,
        38552.79,
        45670.23,
        44094.82,
        35587.19,
        42356.55,
        55808,
        41877,
        55525.94,
        18895.22,
        46800.05,
        42754.45,
        52472,
        38514.44
      ),
      indegree = c(7L,
                   0L, 1L, 7L, 0L, 4L, 5L, 0L, 5L, 0L, 3L, 1L, 0L, 1L, 2L, 5L, 0L),
      outdegree = c(1L, 3L, 4L, 0L, 2L, 2L, 2L, 2L, 5L, 0L, 3L,
                    0L, 5L, 1L, 8L, 2L, 1L),
      iso3 = c(
        "AUT",
        "BEL",
        "DNK",
        "FIN",
        "FRA",
        "GRC",
        "DEU",
        "IRL",
        "ITA",
        "LUX",
        "NLD",
        "NOR",
        "PRT",
        "SWE",
        "ESP",
        "CHE",
        "GBR"
      ),
      capital = structure(
        c(
          213L,
          41L,
          53L,
          77L,
          148L,
          18L,
          32L,
          62L,
          169L,
          106L,
          9L,
          140L,
          99L,
          191L,
          107L,
          33L,
          102L
        ),
        levels = c(
          "Abu Dhabi",
          "Abuja",
          "Accra",
          "Adamstown",
          "Addis Ababa",
          "Algiers",
          "Alofi",
          "Amman",
          "Amsterdam",
          "Andorra la Vella",
          "Ankara",
          "Antananarivo",
          "Apia",
          "Ashgabat",
          "Asmara ",
          "Astana",
          "Asuncion",
          "Athens",
          "Avarua",
          "Baghdad",
          "Baku",
          "Bamako",
          "Bandar Seri Begawan",
          "Bangkok",
          "Bangui",
          "Banjul",
          "Basseterre",
          "Beijing",
          "Beirut",
          "Belgrade",
          "Belmopan",
          "Berlin",
          "Bern",
          "Bishkek",
          "Bissau",
          "Bogota",
          "Brasilia",
          "Bratislava",
          "Brazzaville",
          "Bridgetown",
          "Brussels",
          "Bucharest",
          "Budapest",
          "Buenos Aires",
          "Bujumbura",
          "Cairo",
          "Canberra",
          "Caracas",
          "Castries",
          "Chisinau",
          "Colombo",
          "Conakry",
          "Copenhagen",
          "Dakar",
          "Damascus",
          "Dhaka",
          "Dili",
          "Djibouti",
          "Dodoma",
          "Doha",
          "Douglas",
          "Dublin",
          "Dushanbe",
          "Freetown",
          "Funafuti",
          "Gaborone",
          "George Town",
          "Georgetown",
          "Gibraltar",
          "Grand Turk",
          "Guatemala City",
          "Hagatna",
          "Hamilton",
          "Hanoi",
          "Harare",
          "Havana",
          "Helsinki",
          "Honiara",
          "Islamabad",
          "Jakarta",
          "Jamestown",
          "Jerusalem",
          "Juba",
          "Kabul",
          "Kampala",
          "Kathmandu",
          "Khartoum",
          "Kigali",
          "Kingston",
          "Kingstown",
          "Kinshasa",
          "Kuala Lumpur",
          "Kuwait City",
          "Kyiv (Kiev)",
          "La Paz",
          "Libreville",
          "Lilongwe",
          "Lima",
          "Lisbon",
          "Ljubljana",
          "Lome",
          "London",
          "Longyearbyen",
          "Luanda",
          "Lusaka",
          "Luxembourg",
          "Madrid",
          "Majuro",
          "Malabo",
          "Male",
          "Managua",
          "Manama",
          "Manila",
          "Maputo",
          "Marigot",
          "Maseru",
          "Mata-Utu",
          "Mbabane",
          "Melekeok",
          "Mexico City ",
          "Minsk",
          "Mogadishu",
          "Monaco",
          "Monrovia",
          "Montevideo",
          "Moroni",
          "Moscow",
          "Muscat",
          "Nairobi",
          "Nassau",
          "NDjamena",
          "New Delhi",
          "Niamey",
          "Nicosia",
          "Nouakchott",
          "Noumea",
          "Nukualofa",
          "Nuuk",
          "Oranjestad",
          "Oslo",
          "Ottawa",
          "Ouagadougou",
          "Pago Pago",
          "Palikir",
          "Panama City",
          "Papeete ",
          "Paramaribo",
          "Paris",
          "Phnom Penh",
          "Plymouth",
          "Podgorica",
          "Port-au-Prince",
          "Port-Vila",
          "Port Louis",
          "Port Moresby",
          "Port of Spain",
          "Porto-Novo",
          "Prague",
          "Praia",
          "Pretoria",
          "Pyongyang",
          "Quito",
          "Rabat",
          "Rangoon",
          "Reykjavik",
          "Riga",
          "Riyadh",
          "Road Town",
          "Rome",
          "Roseau",
          "Saint-Pierre",
          "Saint Georges",
          "Saint Helier",
          "Saint Johns",
          "Saint Peter Port",
          "Saipan",
          "San Jose",
          "San Juan",
          "San Marino",
          "San Salvador",
          "Sanaa",
          "Santiago",
          "Santo Domingo",
          "Sao Tome",
          "Sarajevo",
          "Seoul",
          "Singapore",
          "Skopje",
          "Sofia",
          "Stanley",
          "Stockholm",
          "Suva",
          "Taipei",
          "Tallinn",
          "Tarawa",
          "Tashkent",
          "Tbilisi",
          "Tegucigalpa",
          "Tehran",
          "The Settlement",
          "The Valley",
          "Thimphu",
          "Tirana",
          "Tokyo",
          "Torshavn",
          "Tripoli ",
          "Tunis",
          "Ulaanbaatar",
          "Vaduz",
          "Valletta",
          "Vatican City",
          "Victoria",
          "Vienna",
          "Vientiane ",
          "Vilnius",
          "Warsaw",
          "Washington, DC",
          "Wellington",
          "West Island",
          "Windhoek",
          "Yamoussoukro",
          "Yaounde",
          "Yerevan",
          "Zagreb"
        ),
        class = "factor"
      ),
      long = c(
        16.37,
        4.33,
        12.58,
        24.93,
        2.33,
        23.73,
        13.4,
        -6.23,
        12.48,
        6.12,
        4.92,
        10.75,
        -9.13,
        18.05,
        -3.68,
        7.47,
        -0.08
      ),
      lat = c(
        48.2,
        50.83,
        55.67,
        60.17,
        48.87,
        37.98,
        52.52,
        53.32,
        41.9,
        49.6,
        52.35,
        59.92,
        38.72,
        59.33,
        40.4,
        46.92,
        51.5
      )
    ),
    class = "data.frame",
    row.names = c(NA, -17L)
  )


edge_list <- structure(
  list(
    country1 = c(
      "Austria",
      "Belgium",
      "Belgium",
      "Belgium",
      "Denmark",
      "Denmark",
      "Denmark",
      "Denmark",
      "France",
      "France",
      "Germany",
      "Germany",
      "Greece",
      "Greece",
      "Ireland",
      "Ireland",
      "Italy",
      "Italy",
      "Italy",
      "Italy",
      "Italy",
      "Netherlands",
      "Netherlands",
      "Netherlands",
      "Portugal",
      "Portugal",
      "Portugal",
      "Portugal",
      "Portugal",
      "Spain",
      "Spain",
      "Spain",
      "Spain",
      "Spain",
      "Spain",
      "Spain",
      "Spain",
      "Sweden",
      "Switzerland",
      "Switzerland",
      "United Kingdom"
    ),
    country2 = c(
      "Finland",
      "Italy",
      "Netherlands",
      "Spain",
      "Austria",
      "Finland",
      "Greece",
      "Italy",
      "Norway",
      "Spain",
      "Finland",
      "Switzerland",
      "Austria",
      "Switzerland",
      "Austria",
      "Finland",
      "Austria",
      "Finland",
      "Germany",
      "Greece",
      "Switzerland",
      "Germany",
      "Italy",
      "Sweden",
      "Germany",
      "Greece",
      "Italy",
      "Netherlands",
      "Switzerland",
      "Austria",
      "Denmark",
      "Finland",
      "Germany",
      "Greece",
      "Italy",
      "Netherlands",
      "Switzerland",
      "Austria",
      "Austria",
      "Finland",
      "Germany"
    ),
    difference = c(
      1L,
      1L,
      1L,
      1L,
      1L,
      1L,
      1L,
      1L,
      1L,
      1L,
      1L,
      1L,
      1L,
      1L,
      1L,
      1L,
      1L,
      1L,
      1L,
      1L,
      1L,
      1L,
      1L,
      1L,
      1L,
      1L,
      1L,
      1L,
      1L,
      1L,
      1L,
      1L,
      1L,
      1L,
      1L,
      1L,
      1L,
      1L,
      1L,
      1L,
      1L
    ),
    correlation = c(
      0.668950108875404,
      0.530220376463286,
      0.602603105815811,
      0.633894889901975,
      0.518996320049688,
      0.410621575904769,
      0.437541816071113,
      0.472938278950377,
      0.615664539239391,
      0.449799814115612,
      0.483598285245482,
      0.542463675199482,
      0.503746498211967,
      0.419154922709794,
      0.506287535418966,
      0.421561563989063,
      0.658838736064947,
      0.554557263433874,
      0.609431783862321,
      0.802671376462743,
      0.529923094805411,
      0.537097231557396,
      0.641754855493876,
      0.447827308533041,
      0.420920418387666,
      0.476851900398108,
      0.448455600417252,
      0.55536271016908,
      0.421037045545927,
      0.654889436091643,
      0.628137147476826,
      0.553861093006277,
      0.60965723943248,
      0.668105346874185,
      0.86414775331133,
      0.554894952278287,
      0.650120680883999,
      0.446939183067011,
      0.608148297554411,
      0.409046621960521,
      0.401572675034778
    )
  ),
  class = "data.frame",
  row.names = c(NA,
                -41L)
)

n <- igraph::graph_from_data_frame(edge_list, directed = T, vertices = node_list )

gr <-tidygraph::as_tbl_graph(n)

# Constructing the map

map <- ggraph::ggraph(
  gr,
  x = long,
  y = lat
) +
  geom_sf(
    data = Europe,
    fill = "grey10",
    color = "white",
    linewidth = .3,
  ) +
  geom_node_point(size= 2, colour = "#FFFF00")+
  ggraph::geom_edge_bundle_path(aes(colour = ..x..),
                                alpha = .8,
                                width = .2
  ) +
  scale_colour_gradient(low = "#FFFF00", high = "#FA57B1")+
  coord_sf(xlim = c(-20,30), ylim = c(30,65), expand = FALSE)+
  theme_void()
map

我确实让 colored颜色 的纬度依赖,因为这是我得到的最接近的 colored颜色 ,但这不是必要的!EDGE_LIST包含4个变量,其中一个变量可以忽略不计,因为它被用来创建定向数据集,并且每个边的值都是"1".其他三个是国家1和国家2,第三个是相关性,表明国家之间的毒品交易量. node 列表包含经度和纬度以及某些其他国家/地区的特征.我对和平准则表示歉意.我真的很感激你的帮助,因为我已经在这个问题上挣扎了很长一段时间.我是新来的,我希望这对你有意义.提前谢谢!

推荐答案

要根据方向为边缘上色,请使用after_stat将数字index映射到colour上.此外,要设置边的 colored颜色 ,请使用ggraph::scale_edge_color_gradient:

library(tidyverse)
library(sf)
library(rnaturalearth)
library(ggraph)

ggraph::ggraph(
  gr,
  x = long,
  y = lat
) +
  geom_sf(
    data = Europe,
    fill = "grey10",
    color = "white",
    linewidth = .3,
  ) +
  geom_node_point(size = 2, colour = "#FFFF00") +
  ggraph::geom_edge_bundle_path(
    aes(colour = after_stat(index)),
    alpha = .8,
    width = 1
  ) +
  ggraph::scale_edge_color_gradient(
    low = "#FFFF00", high = "#FA57B1"
  ) +
  coord_sf(
    xlim = c(-20, 30),
    ylim = c(30, 65), 
    expand = FALSE
  ) +
  theme_void()

enter image description here

R相关问答推荐

如何将具有重复名称的收件箱合并到R中的另一列中,而结果不同?

将模拟变量乘以多个观测结果中的模拟变量

在值和NA的行顺序中寻找中断模式

为什么当我try 在收件箱中使用合并功能时会出现回收错误?

为什么st_join(ob1,ob2,left = True)返回具有比ob1更多功能的sf对象?

使用tidyverse / Mutate的存款账户余额

咕噜中的元素列表:map

根据R中两个变量的两个条件删除带有dspirr的行

如何优化向量的以下条件赋值?

从BRM预测价值

在df中保留原始变量和新变量

可以替代与NSE一起使用的‘any_of()’吗?

为了网络分析目的,将数据框转换为长格式列联表

为什么在BASE R中绘制线条时会看到线上的点?

如何在ggplot2中创建多个y轴(每个变量一个)

如何删除设置大小的曲线图并添加条形图顶部数字的百分比

随机 Select 的非NA列的行均数

按组使用dummy r获取高于标准的行的平均值

如何准确地指出Read_delim所面临的问题?

在子图内和子图之间对齐行数不均匀的表格罗布对