美文网首页
R可视化——议会图(parliament diagrams)的绘

R可视化——议会图(parliament diagrams)的绘

作者: 科研那点事儿 | 来源:发表于2022-09-25 19:49 被阅读0次

         今天给大家所要展示的图形是一种柱形图的变形图表——议会图(parliament diagrams)!

    安装、加载R包

    #安装包
    install.packages("ggparliament")
    install.packages("tidyverse")
    #加载包
    library(ggparliament)
    library(tidyverse)
    

    数据

    使用ggparliament包自带的数据election_data:

    df<-election_data %>% 
      filter(country == "Russia" & year == 2016)
    
    image.png

    使用parliament_data()函数将数据转换成绘图所需要的格式:

    df1 <- parliament_data(election_data = df,
                                     type = "semicircle", # 议会类型
                                     parl_rows = 10,      # 议会的行数
                                     party_seats = df$seats) # 席位
    
    image.png

    绘图

    ggplot(df1, aes(x = x, y = y, colour = party_short)) +
      geom_parliament_seats() + 
      geom_highlight_government(government == 1) +
      geom_parliament_bar(colour = colour, party = party_long, label = TRUE) +#使用条形图显示比例
      draw_majoritythreshold(n = 225, label = TRUE, type = "semicircle") +#添加阈值线
      theme_ggparliament() +
      labs(title = "R") +#标题
      scale_colour_manual(values = df1$colour, 
                          limits = df1$party_short) +#颜色
      draw_partylabels(type = "semicircle",   ##标签
                       party_names = party_long,
                       party_seats = seats,
                       party_colours = colour)+
      draw_totalseats(n = 450, type = "semicircle")#标签
    
    image.png

    其他类型展示

    处理数据时,在type参数中修改相应类型即可绘制不同类型会议图:

    df2 <- parliament_data(election_data = df,
                           type = "circle", # 议会类型
                           parl_rows = 10,      # 议会的行数
                           party_seats = df$seats) # 席位
    df3 <- parliament_data(election_data = df,
                           type = "classroom", # 议会类型
                           parl_rows = 11,      # 议会的行数
                           party_seats = df$seats) # 席位
    df4 <- parliament_data(election_data = df,
                           type = "horseshoe", # 议会类型
                           parl_rows = 10,      # 议会的行数
                           party_seats = df$seats) # 席位
    
    ggplot(df2, aes(x = x, y = y, color = party_short)) +
      geom_parliament_seats() + 
      theme_ggparliament() +
      labs(title = "Russia, 2016") +
      scale_colour_manual(values = df1$colour, 
                          limits = df1$party_short)
    
    image.png
    ggplot(df3, aes(x = x, y = y, color = party_short)) +
      geom_parliament_seats() + 
      theme_ggparliament() +
      labs(title = "Russia, 2016") +
      scale_colour_manual(values = df1$colour, 
                          limits = df1$party_short)
    
    image.png
    ggplot(df4, aes(x = x, y = y, color = party_short)) +
      geom_parliament_seats() + 
      theme_ggparliament() +
      labs(title = "Russia, 2016") +
      scale_colour_manual(values = df1$colour, 
                          limits = df1$party_short)
    
    image.png
    参考:https://r-charts.com/part-whole/ggparliament/

    相关文章

      网友评论

          本文标题:R可视化——议会图(parliament diagrams)的绘

          本文链接:https://www.haomeiwen.com/subject/jvizortx.html