R 可视化 网络图
使用「ggplot2」来构建数据绘制网络图,下面来看具体案例操作

安装并加载R包

  1. package.list=c("tidyverse","magrittr","patchwork")
  2. for (package in package.list) {
  3. if (!require(package,character.only=T, quietly=T)) {
  4. install.packages(package)
  5. library(package, character.only=T)
  6. }
  7. }

导入数据构建数据集

  1. plot_data <- read_tsv("data.xls") %>%
  2. mutate(theta = seq(0,2 * pi, length.out = 100),x = 10 * cos(theta),
  3. y = 10 * sin(theta),labx = 12 * cos(theta),
  4. laby = 12 * sin(theta),angle = 360*(theta/(2*pi)))

数据清洗

  1. Creativity <- plot_data %>%
  2. filter(category == "Creativity") %>%
  3. select(x, y) %>%
  4. mutate(id = row_number())
  5. Creativity_edges <- tibble(expand.grid(id1 = Creativity$id, id2 = Creativity$id)) %>%
  6. inner_join(Creativity, by = c("id1" = "id")) %>%
  7. inner_join(Creativity, by = c("id2" = "id")) %>%
  8. select(-c(id1, id2)) %>%
  9. set_colnames(c("x", "y", "xend", "yend"))
  10. Identity <- plot_data %>%
  11. filter(category == "Identity") %>%
  12. select(x, y) %>%
  13. mutate(id = row_number())
  14. Identity_edges <- tibble(expand.grid(id1 = Identity$id, id2 = Identity$id)) %>%
  15. inner_join(Identity, by = c("id1" = "id")) %>%
  16. inner_join(Identity, by = c("id2" = "id")) %>%
  17. select(-c(id1, id2)) %>%
  18. set_colnames(c("x", "y", "xend", "yend"))

数据可视化1

  1. p1 <- ggplot() +
  2. geom_segment(data = Creativity_edges,mapping = aes(x = x, y = y, xend = xend, yend = yend),
  3. colour = alpha("#884c94", 0.5), size = 0.05) +
  4. geom_point(data = plot_data,mapping = aes(x = x, y = y, colour = category),size =1) +
  5. geom_text(data = plot_data,
  6. mapping = aes(x = labx, y = laby, label = name, angle = angle, colour = category),
  7. family = "ubuntu", hjust = 0, size =4) +
  8. scale_colour_manual("", values = c("white", "#884c94", "#26aa83", "#4a75b0", "#ff3377")) +
  9. labs(subtitle = "Creativity") +xlim(-20, 20) +
  10. ylim(-20, 20) +
  11. coord_fixed() +
  12. theme_void() +
  13. theme(legend.position = "none",
  14. legend.text = element_text(hjust = 0.5, size = 12, color = "white"),
  15. plot.subtitle = element_text(hjust = 0.5, size = 18, color = "black"),
  16. plot.background = element_rect(fill = "white", colour="white"),
  17. panel.background = element_rect(fill = "white", colour="white"),
  18. plot.margin = unit(c(0,0,0,0), "cm"))

数据可视化2

  1. p2 <- ggplot() +
  2. geom_segment(data = Identity_edges,mapping = aes(x = x, y = y, xend = xend, yend = yend),
  3. colour = alpha("#26aa83",0.5), size = 0.05) +
  4. geom_point(data = plot_data,mapping = aes(x = x, y = y, colour = category),size = 1) +
  5. geom_text(data = plot_data,
  6. mapping = aes(x = labx, y = laby, label = name, angle = angle, colour = category),
  7. hjust = 0,size =4) +
  8. scale_colour_manual("", values = c("white","#884c94", "#26aa83", "#4a75b0","#ff3377")) +
  9. labs(subtitle = "Identity") +
  10. xlim(-20, 20) +
  11. ylim(-20, 20) +
  12. coord_fixed() +
  13. theme_void() +
  14. theme(legend.position = "none",
  15. plot.subtitle = element_text(hjust = 0.5, size = 18, color = "black"),
  16. plot.background = element_rect(fill = "white", colour="white"),
  17. panel.background = element_rect(fill = "white", colour="white"),
  18. plot.margin = unit(c(0,0,0,0), "cm"))

拼图

  1. p1 + p2 +
  2. theme(
  3. plot.background = element_rect(fill = "white", colour="white"),
  4. panel.background = element_rect(fill = "white", colour="white"))

网络图绘制 - 图1