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

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

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没有子圆圈

如何修复?谢谢!

library(ggraph)
library(igraph)
library(tidyverse)
library(viridis)

base <- data.frame(from =c('US','US','UK','UK','DE','DE'),
                   to = c('cat_a','cat_b','cat_a','cat_b','cat_a','cat_b'),
                   sales=c(1,3,5,4,3,1.5))

vetic <- base %>% gather(key='type', value ='dim',c(1:2)) %>%
  group_by(dim) %>% summarise(sales = sum(sales)) 

mdf <- graph_from_data_frame(base,directed = TRUE,vertices = vetic)

ggraph(mdf, layout = 'circlepack', weight=sales) + 
  geom_node_circle(aes(fill = depth)) +
    geom_node_text(size=10,aes(label= paste0(name,'\n',sales)))+
  theme_void()+scale_fill_distiller(palette = "RdPu")

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


<details>
<summary>英文:</summary>

`ggraph/igraph`, the result in below plot isn&#39;t correct :

 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;)  

2) there is no sub circle for UK/US

How to fix it ? Thanks!








    library(ggraph)
    library(igraph)
    library(tidyverse)
    library(viridis)
    
    base &lt;- data.frame(from =c(&#39;US&#39;,&#39;US&#39;,&#39;UK&#39;,&#39;UK&#39;,&#39;DE&#39;,&#39;DE&#39;),
                       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;),
                       sales=c(1,3,5,4,3,1.5))
    
    vetic &lt;- base %&gt;% gather(key=&#39;type&#39;, value =&#39;dim&#39;,c(1:2)) %&gt;% 
      group_by(dim) %&gt;% summarise(sales = sum(sales)) 
    
    mdf &lt;- graph_from_data_frame(base,directed = TRUE,vertices = vetic)
    
    
    ggraph(mdf, layout = &#39;circlepack&#39;, weight=sales) + 
      geom_node_circle(aes(fill = depth)) +
        geom_node_text(size=10,aes(label= paste0(name,&#39;\n&#39;,sales)))+
      theme_void()+scale_fill_distiller(palette = &quot;RdPu&quot;) 

[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/WfV1F.png

</details>


# 答案1
**得分**: 3

问题出在你创建图表的方式上。似乎你想要展示每个国家内的“cat_a”和“cat_b”的数量,但你创建的图表好像将“cat_a”和“cat_b”看作单个节点。如果你想要在每个国家内有一个“cat_a”和一个“cat_b”节点,那么你需要将它们指定为不同的节点。

例如,我们可以这样做:

``` r
library(ggraph)
library(igraph)
library(tidyverse)
library(viridis)

base <- data.frame(from =c('US','US','UK','UK','DE','DE'),
                   to = c('cat_a','cat_b','cat_a','cat_b','cat_a','cat_b'),
                   sales=c(1,3,5,4,3,1.5))

base_fixed <- base %>% mutate(to = paste(from, to)) 

这将给出以下数据框:

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

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

vertices <- base_fixed %>%
  group_by(from) %>%
  summarize(sales = sum(sales)) %>%
  rbind(base_fixed %>% select(-1) %>% rename(from = to))

vertices
#> # A tibble: 9 x 2
#>   from     sales
#>   <chr>    <dbl>
#> 1 DE         4.5
#> 2 UK         9  
#> 3 US         4  
#> 4 US cat_a   1  
#> 5 US cat_b   3  
#> 6 UK cat_a   5  
#> 7 UK cat_b   4  
#> 8 DE cat_a   3  
#> 9 DE cat_b   1.5

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

graph_from_data_frame(base_fixed, vertices = vertices) %>%
  ggraph(layout = 'circlepack', weight = sales) + 
  geom_node_circle(aes(fill = depth)) +
  geom_node_text(size = 5, 
                 aes(label = ifelse(depth == 0, '',
                      paste0(substr(name, 4, 20), '\n', sales)))) +
  geom_node_label(size = 6, aes(label = ifelse(depth == 1, NA,
                   substr(name, 1, 2)))) +
  theme_void() +
  scale_fill_distiller(palette = "RdPu", guide = 'none') +
  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:

library(ggraph)
library(igraph)
library(tidyverse)
library(viridis)

base &lt;- data.frame(from =c(&#39;US&#39;,&#39;US&#39;,&#39;UK&#39;,&#39;UK&#39;,&#39;DE&#39;,&#39;DE&#39;),
                   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;),
                   sales=c(1,3,5,4,3,1.5))

base_fixed &lt;- base %&gt;% mutate(to = paste(from, to)) 

Which gives the following data frame:

base_fixed
#&gt;   from       to sales
#&gt; 1   US US cat_a   1.0
#&gt; 2   US US cat_b   3.0
#&gt; 3   UK UK cat_a   5.0
#&gt; 4   UK UK cat_b   4.0
#&gt; 5   DE DE cat_a   3.0
#&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):

vertices &lt;- base_fixed %&gt;%
  group_by(from) %&gt;%
  summarize(sales = sum(sales)) %&gt;%
  rbind(base_fixed %&gt;% select(-1) %&gt;% rename(from = to))

vertices
#&gt; # A tibble: 9 x 2
#&gt;   from     sales
#&gt;   &lt;chr&gt;    &lt;dbl&gt;
#&gt; 1 DE         4.5
#&gt; 2 UK         9  
#&gt; 3 US         4  
#&gt; 4 US cat_a   1  
#&gt; 5 US cat_b   3  
#&gt; 6 UK cat_a   5  
#&gt; 7 UK cat_b   4  
#&gt; 8 DE cat_a   3  
#&gt; 9 DE cat_b   1.5

The graph could then be drawn as follows:

graph_from_data_frame(base_fixed, vertices = vertices) %&gt;%
  ggraph(layout = &#39;circlepack&#39;, weight = sales) + 
  geom_node_circle(aes(fill = depth)) +
  geom_node_text(size = 5, 
                 aes(label = ifelse(depth == 0, &#39;&#39;,
                      paste0(substr(name, 4, 20), &#39;\n&#39;, sales)))) +
  geom_node_label(size = 6, aes(label = ifelse(depth == 1, NA,
                   substr(name, 1, 2)))) +
  theme_void() +
  scale_fill_distiller(palette = &quot;RdPu&quot;, guide = &#39;none&#39;) +
  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:

确定