ggraph:调整圆形树状图上的文本标签位置

问题描述 投票:0回答:1

我一直在试图弄清楚如何修复圆形树状图的标签文本位置。我一直在关注here发布的代码,但无法弄清楚问题是什么。

这就是我的圆形树状图目前的样子: enter image description here 但我希望文本标签看起来像这样: enter image description here

如何解决?我一直严格遵循代码。

library(tidyverse)
library(ggraph)
library(igraph)
library(RColorBrewer)
# PLOT 
ggraph(mygraph, layout = 'dendrogram', circular = TRUE) +
  geom_edge_diagonal(colour="grey") +
  scale_edge_colour_distiller(palette = "RdPu") +
  geom_node_text(aes(x = x*1.15,
                       y=y*1.15,
                       filter = leaf,
                       label = name,
                       angle = angle,
                       hjust = hjust,
                       colour = group), size=2.7, alpha=1) +
  geom_node_point(aes(filter = leaf,
                      x = x*1.07,
                      y=y*1.07,
                      colour=group,
                      size=value,
                      alpha=0.5)) +
  scale_colour_manual(values= rep( brewer.pal(9,"Paired") , 30)) +
  scale_size_continuous( range = c(0.1,10) ) +
  theme_void() +
  theme(
    legend.position="none",
    plot.margin=unit(c(0,0,0,0),"cm"),
  ) +
  expand_limits(x = c(-1.3, 1.3), y = c(-1.3, 1.3))

下面是ggraph数据。

> dput(mygraph) structure(list(75, TRUE, c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 2, 1, 3, 1, 5, 6, 7, 2, 8, 9, 3, 10, 10, 11, 11, 5, 4, 3, 10, 2, 1, 7, 9, 4, 11, 1, 3, 3, 2, 5, 3, 7, 3, 3, 2, 10, 5, 2, 7, 7, 3, 4, 9, 3, 2, 9, 5, 7, 4, 3,
10, 5, 5, 3, 3, 9, 9, 2, 4, 9, 9, 9, 9, 1, 9, 2, 2, 12, 3, 10, 5, 9, 3, 5, 3, 4, 2, 5), c(4, 3, 11, 2, 9, 6, 5, 12, 7, 1, 10, 8, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 16, 34, 35, 36, 37, 38, 39, 40, 41, 42,
15, 43, 28, 44, 15, 45, 46, 47, 48, 49, 50, 51, 52, 53, 15, 54, 55, 28, 48, 56, 57, 58, 16, 59, 60, 61, 62, 28, 59, 63, 64, 48, 16, 39, 65, 66, 67, 68, 69, 48, 18, 70, 46, 71, 72, 39, 59, 72, 73, 16, 48, 74 ), NULL, NULL, NULL, NULL, list(c(1, 0, 1),
structure(list(), names = character(0)), list(name = c("origin", "Outdoor activities", "Domestic activities", "Arts and craft activities", "Agricultural activities", "Learning activities", "Health and wellbeing activities", "Musical activities", "Workshop
activities", "Entertainment activities", "Physical and fitness activities", "Community activities", "Mechanical activities", "Bush walking", "Baking", "Manual arts and crafts", "Community gardening", "Historical sharing Q&A", "Clothing maintenance (sewing)",
"Wild life tours", "Carpentry", "Assisted guides (student companionship)", "Knowledge passing", "Palative awareness", "Community musical bands", "Parenting skills", "General workshops", "Community theatre", "Crochet and knitting", "Water aerobics", "Gentle
physical exercise", "Community diners (meals on wheels)", "Inala homework club", "Language club", "Photography", "Physical exercises (dance, gym, colour run)", "Pet daycare", "Trivia camp", "Music lessons", "Board games", "Growing vegetables", "Cooking
cafe", "Schools camps and day visits", "Painting and sipping", "Book club", "Composing music", "Woodworking", "Cake decorating", "Cooking", "Fitness training", "Students teaching IT", "Sharing traditional recipes", "Playing music", "Traditional dancing",
"Dragon's den (chicken eggs)", "Chess", "Card games", "Tutoring", "Playing musical instruments", "Painting", "Rehabilitation and physio", "Using technology (incl. social media)", "Reading together", "Video games", "Karaoke", "Putt putt", "Table tennies",
"Playing puzzles", "Walking together", "Watching sports on the television", "Learning mechanical work", "Water sports", "Story telling", "Interpretative painting", "Life skills"), value = c(0.585580174112692, 0.893185549415648, 0.26200358569622, 0.203951701987535,
0.488889964995906, 0.58272284292616, 0.512202943209559, 0.486715000122786, 0.0233161903452128, 0.725962040247396, 0.230725078377873, 0.0891078060958534, 0.601971111027524, 0.770974726416171, 0.112899012630805, 0.702898587333038, 0.716850318480283, 0.131957670208067,
0.65517760720104, 0.910050076199695, 0.095754484878853, 0.610197829082608, 0.0592852185945958, 0.822226302931085, 0.479145678458735, 0.460636418079957, 0.704600618453696, 0.521227885968983, 0.175805907929316, 0.640184475807473, 0.45457612676546, 0.507625086698681,
0.460464909672737, 0.791949789505452, 0.679316321620718, 0.956594054820016, 0.107164902845398, 0.13667330192402, 0.956593341659755, 0.416489312890917, 0.994603507919237, 0.465922170784324, 0.166321847587824, 0.625731714069843, 0.0725796180777252, 0.963703070301563,
0.42543631256558, 0.521754823392257, 0.574694129405543, 0.888251390540972, 0.956966195488349, 0.631142220227048, 0.81483043031767, 0.999659575987607, 0.817718225996941, 0.994496389990672, 0.354073196649551, 0.502177250571549, 0.377560483757406, 0.315485945204273,
0.7389785728883, 0.218990557361394, 0.346426092088223, 0.229574259370565, 0.538299076724797, 0.135757425567135, 0.236583026824519, 0.159444775199518, 0.915375969838351, 0.191791382618248, 0.747529144166037, 0.157402683980763, 0.537972879828885, 0.872691323980689,
0.482101482339203), group = c(NA, "origin", "origin", "origin", "origin", "origin", "origin", "origin", "origin", "origin", "origin", "origin", "origin", "Outdoor activities", "Domestic activities", "Arts and craft activities", "Agricultural activities",
"Learning activities", "Domestic activities", "Outdoor activities", "Arts and craft activities", "Outdoor activities", "Learning activities", "Health and wellbeing activities", "Musical activities", "Domestic activities", "Workshop activities", "Entertainment
activities", "Arts and craft activities", "Physical and fitness activities", "Physical and fitness activities", "Community activities", "Community activities", "Learning activities", "Arts and craft activities", "Physical and fitness activities", "Domestic
activities", "Outdoor activities", "Musical activities", "Entertainment activities", "Agricultural activities", "Community activities", "Outdoor activities", "Arts and craft activities", "Learning activities", "Musical activities", "Arts and craft activities",
"Arts and craft activities", "Domestic activities", "Physical and fitness activities", "Learning activities", "Domestic activities", "Musical activities", "Musical activities", "Agricultural activities", "Entertainment activities", "Entertainment activities",
"Learning activities", "Musical activities", "Arts and craft activities", "Physical and fitness activities", "Learning activities", "Learning activities", "Entertainment activities", "Entertainment activities", "Entertainment activities", "Entertainment
activities", "Entertainment activities", "Outdoor activities", "Entertainment activities", "Mechanical activities", "Physical and fitness activities", "Learning activities", "Arts and craft activities", "Learning activities"), id = c(NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L,
47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L), angle = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 84.1935483870968, 78.3870967741936, 72.5806451612903, 66.7741935483871, 60.9677419354839, 55.1612903225806,
49.3548387096774, 43.5483870967742, 37.741935483871, 31.9354838709677, 26.1290322580645, 20.3225806451613, 14.5161290322581, 8.70967741935483, 2.90322580645162, -2.90322580645162, -8.70967741935483, -14.5161290322581, -20.3225806451613, -26.1290322580645,
-31.9354838709677, -37.741935483871, -43.5483870967742, -49.3548387096774, -55.1612903225806, -60.9677419354839, -66.7741935483871, -72.5806451612903, -78.3870967741935, -84.1935483870968, -90, 84.1935483870968, 78.3870967741935, 72.5806451612903, 66.7741935483871,
60.9677419354839, 55.1612903225806, 49.3548387096774, 43.5483870967742, 37.741935483871, 31.9354838709677, 26.1290322580645, 20.3225806451613, 14.5161290322581, 8.70967741935482, 2.90322580645159, -2.90322580645159, -8.70967741935482, -14.516129032258,
-20.3225806451613, -26.1290322580645, -31.9354838709677, -37.741935483871, -43.5483870967742, -49.3548387096774, -55.1612903225807, -60.9677419354838, -66.7741935483871, -72.5806451612903, -78.3870967741935, -84.1935483870968, -90), hjust = c(NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), structure(list(), names
= character(0))),
<environment>), class = "igraph")

r dendrogram ggraph
1个回答
0
投票

看起来你搞乱了标签角度的计算。为了解决这个问题,我使用一些三角学来根据节点的 x 和 y 位置动态计算角度:

library(tidyverse)
library(ggraph)
library(igraph)
library(RColorBrewer)

mygraph <- upgrade_graph(mygraph)

ggraph(mygraph, layout = "dendrogram", circular = TRUE) +
  geom_edge_diagonal(colour = "grey") +
  scale_edge_colour_distiller(palette = "RdPu") +
  geom_node_text(aes(
    x = x * 1.15,
    y = y * 1.15,
    filter = leaf,
    label = name,
    angle = ifelse(
      x >= 0,
      asin(y) * 360 / 2 / pi,
      360 - asin(y) * 360 / 2 / pi
    ),
    hjust = ifelse(
      x >= 0, 0, 1
    ),
    colour = group
  ), size = 1.3, alpha = 1, vjust = .5, color = "black") +
  geom_node_point(aes(
    filter = leaf,
    x = x * 1.07,
    y = y * 1.07,
    colour = group,
    size = value,
    alpha = 0.5
  )) +
  scale_colour_manual(values = rep(brewer.pal(9, "Paired"), 30)) +
  scale_size_continuous(range = c(0.1, 10)) +
  theme_void() +
  theme(
    legend.position = "none",
    plot.margin = unit(c(0, 0, 0, 0), "cm"),
  ) +
  expand_limits(x = c(-1.3, 1.3), y = c(-1.3, 1.3)) +
  coord_equal()

© www.soinside.com 2019 - 2024. All rights reserved.