英文:
ggraph/igraph show uncorrece label and hierarchical structure
问题
ggraph/igraph,以下绘图结果不正确:
- 
DE的数量为4.5,但子圆圈分别为8.8和9(正确数量应为“DE cat_a 3.0,DE cat_b 1.5”)
 - 
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")
<details>
<summary>英文:</summary>
`ggraph/igraph`, the result in below plot isn't correct :
 1)The amount of DE is 4.5, but the sub circles are 8.8 and 9 (the correct amount should be "DE cat_a   3.0, DE cat_b   1.5")  
2) there is no sub circle for UK/US
How to fix it ? Thanks!
    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") 
[![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()
创建于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 <- 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)) 
Which gives the following data frame:
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
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 <- 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
The graph could then be drawn as follows:
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()
<sup>Created on 2023-05-15 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。



评论