ggraph/igraph显示不正确的标签和分层结构

huangapple go评论95阅读模式
英文:

ggraph/igraph show uncorrece label and hierarchical structure

问题

ggraph/igraph,以下绘图结果不正确:

  1. DE的数量为4.5,但子圆圈分别为8.8和9(正确数量应为“DE cat_a 3.0,DE cat_b 1.5”)

  2. UK/US没有子圆圈

如何修复?谢谢!

  1. library(ggraph)
  2. library(igraph)
  3. library(tidyverse)
  4. library(viridis)
  5. base <- data.frame(from =c('US','US','UK','UK','DE','DE'),
  6. to = c('cat_a','cat_b','cat_a','cat_b','cat_a','cat_b'),
  7. sales=c(1,3,5,4,3,1.5))
  8. vetic <- base %>% gather(key='type', value ='dim',c(1:2)) %>%
  9. group_by(dim) %>% summarise(sales = sum(sales))
  10. mdf <- graph_from_data_frame(base,directed = TRUE,vertices = vetic)
  11. ggraph(mdf, layout = 'circlepack', weight=sales) +
  12. geom_node_circle(aes(fill = depth)) +
  13. geom_node_text(size=10,aes(label= paste0(name,'\n',sales)))+
  14. theme_void()+scale_fill_distiller(palette = "RdPu")

ggraph/igraph显示不正确的标签和分层结构

  1. <details>
  2. <summary>英文:</summary>
  3. `ggraph/igraph`, the result in below plot isn&#39;t correct :
  4. 1)The amount of DE is 4.5, but the sub circles are 8.8 and 9 (the correct amount should be &quot;DE cat_a 3.0, DE cat_b 1.5&quot;)
  5. 2) there is no sub circle for UK/US
  6. How to fix it ? Thanks!
  7. library(ggraph)
  8. library(igraph)
  9. library(tidyverse)
  10. library(viridis)
  11. base &lt;- data.frame(from =c(&#39;US&#39;,&#39;US&#39;,&#39;UK&#39;,&#39;UK&#39;,&#39;DE&#39;,&#39;DE&#39;),
  12. to = c(&#39;cat_a&#39;,&#39;cat_b&#39;,&#39;cat_a&#39;,&#39;cat_b&#39;,&#39;cat_a&#39;,&#39;cat_b&#39;),
  13. sales=c(1,3,5,4,3,1.5))
  14. vetic &lt;- base %&gt;% gather(key=&#39;type&#39;, value =&#39;dim&#39;,c(1:2)) %&gt;%
  15. group_by(dim) %&gt;% summarise(sales = sum(sales))
  16. mdf &lt;- graph_from_data_frame(base,directed = TRUE,vertices = vetic)
  17. ggraph(mdf, layout = &#39;circlepack&#39;, weight=sales) +
  18. geom_node_circle(aes(fill = depth)) +
  19. geom_node_text(size=10,aes(label= paste0(name,&#39;\n&#39;,sales)))+
  20. theme_void()+scale_fill_distiller(palette = &quot;RdPu&quot;)
  21. [![enter image description here][1]][1]
  22. [1]: https://i.stack.imgur.com/WfV1F.png
  23. </details>
  24. # 答案1
  25. **得分**: 3
  26. 问题出在你创建图表的方式上。似乎你想要展示每个国家内的“cat_a”和“cat_b”的数量,但你创建的图表好像将“cat_a”和“cat_b”看作单个节点。如果你想要在每个国家内有一个“cat_a”和一个“cat_b”节点,那么你需要将它们指定为不同的节点。
  27. 例如,我们可以这样做:
  28. ``` r
  29. library(ggraph)
  30. library(igraph)
  31. library(tidyverse)
  32. library(viridis)
  33. base <- data.frame(from =c('US','US','UK','UK','DE','DE'),
  34. to = c('cat_a','cat_b','cat_a','cat_b','cat_a','cat_b'),
  35. sales=c(1,3,5,4,3,1.5))
  36. base_fixed <- base %>% mutate(to = paste(from, to))

这将给出以下数据框:

  1. base_fixed
  2. #> from to sales
  3. #> 1 US US cat_a 1.0
  4. #> 2 US US cat_b 3.0
  5. #> 3 UK UK cat_a 5.0
  6. #> 4 UK UK cat_b 4.0
  7. #> 5 DE DE cat_a 3.0
  8. #> 6 DE DE cat_b 1.5

同样,顶点应该为每个生成的9个节点(3个国家节点,以及每个国家内的两个“cat”节点)具有标签和值:

  1. vertices <- base_fixed %>%
  2. group_by(from) %>%
  3. summarize(sales = sum(sales)) %>%
  4. rbind(base_fixed %>% select(-1) %>% rename(from = to))
  5. vertices
  6. #> # A tibble: 9 x 2
  7. #> from sales
  8. #> <chr> <dbl>
  9. #> 1 DE 4.5
  10. #> 2 UK 9
  11. #> 3 US 4
  12. #> 4 US cat_a 1
  13. #> 5 US cat_b 3
  14. #> 6 UK cat_a 5
  15. #> 7 UK cat_b 4
  16. #> 8 DE cat_a 3
  17. #> 9 DE cat_b 1.5

然后,可以按照以下方式绘制图表:

  1. graph_from_data_frame(base_fixed, vertices = vertices) %>%
  2. ggraph(layout = 'circlepack', weight = sales) +
  3. geom_node_circle(aes(fill = depth)) +
  4. geom_node_text(size = 5,
  5. aes(label = ifelse(depth == 0, '',
  6. paste0(substr(name, 4, 20), '\n', sales)))) +
  7. geom_node_label(size = 6, aes(label = ifelse(depth == 1, NA,
  8. substr(name, 1, 2)))) +
  9. theme_void() +
  10. scale_fill_distiller(palette = "RdPu", guide = 'none') +
  11. coord_equal()

ggraph/igraph显示不正确的标签和分层结构

创建于2023-05-15,使用 reprex v2.0.2

英文:

The problem lies with how you are creating your graph. It seems you want to show the amount of cat_a and cat_b within each country, but you are creating the graph as though cat_a and cat_b are each a single node. If you want a cat_a and a cat_b node inside each country, then you need to specify them as distinct nodes.

For example, we can do:

  1. library(ggraph)
  2. library(igraph)
  3. library(tidyverse)
  4. library(viridis)
  5. base &lt;- data.frame(from =c(&#39;US&#39;,&#39;US&#39;,&#39;UK&#39;,&#39;UK&#39;,&#39;DE&#39;,&#39;DE&#39;),
  6. to = c(&#39;cat_a&#39;,&#39;cat_b&#39;,&#39;cat_a&#39;,&#39;cat_b&#39;,&#39;cat_a&#39;,&#39;cat_b&#39;),
  7. sales=c(1,3,5,4,3,1.5))
  8. base_fixed &lt;- base %&gt;% mutate(to = paste(from, to))

Which gives the following data frame:

  1. base_fixed
  2. #&gt; from to sales
  3. #&gt; 1 US US cat_a 1.0
  4. #&gt; 2 US US cat_b 3.0
  5. #&gt; 3 UK UK cat_a 5.0
  6. #&gt; 4 UK UK cat_b 4.0
  7. #&gt; 5 DE DE cat_a 3.0
  8. #&gt; 6 DE DE cat_b 1.5

Similarly, the vertices should have a label and value for each of the resulting 9 nodes (3 country nodes, plus two "cat" nodes within each):

  1. vertices &lt;- base_fixed %&gt;%
  2. group_by(from) %&gt;%
  3. summarize(sales = sum(sales)) %&gt;%
  4. rbind(base_fixed %&gt;% select(-1) %&gt;% rename(from = to))
  5. vertices
  6. #&gt; # A tibble: 9 x 2
  7. #&gt; from sales
  8. #&gt; &lt;chr&gt; &lt;dbl&gt;
  9. #&gt; 1 DE 4.5
  10. #&gt; 2 UK 9
  11. #&gt; 3 US 4
  12. #&gt; 4 US cat_a 1
  13. #&gt; 5 US cat_b 3
  14. #&gt; 6 UK cat_a 5
  15. #&gt; 7 UK cat_b 4
  16. #&gt; 8 DE cat_a 3
  17. #&gt; 9 DE cat_b 1.5

The graph could then be drawn as follows:

  1. graph_from_data_frame(base_fixed, vertices = vertices) %&gt;%
  2. ggraph(layout = &#39;circlepack&#39;, weight = sales) +
  3. geom_node_circle(aes(fill = depth)) +
  4. geom_node_text(size = 5,
  5. aes(label = ifelse(depth == 0, &#39;&#39;,
  6. paste0(substr(name, 4, 20), &#39;\n&#39;, sales)))) +
  7. geom_node_label(size = 6, aes(label = ifelse(depth == 1, NA,
  8. substr(name, 1, 2)))) +
  9. theme_void() +
  10. scale_fill_distiller(palette = &quot;RdPu&quot;, guide = &#39;none&#39;) +
  11. coord_equal()

ggraph/igraph显示不正确的标签和分层结构

<sup>Created on 2023-05-15 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年5月15日 10:07:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76250463.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定