【问题标题】:How to make this graph - that compares ranks - in R?如何在 R 中制作这个图表 - 比较排名?
【发布时间】:2021-12-12 20:45:57
【问题描述】:

我正在尝试在 R 中制作一个类似于图片上的图形。我尝试使用这段代码,但它看起来不一样,我希望它像图片上的一样对称.

我的 data.frame 如下所示:

Group    Ranking1    Ranking2     Pop
  a           1            1      12345
  b           2            4      127868
  c           3            2      123477
  d           4            3      9485
  e           5            7      132588
  f           6            5      38741
  g           7            9      8372
  h           8            11     53423
  i           9            6      238419
  j           10           16     31314

而我使用的代码是:

ggparcoord(data,
columns = 2:3, groupColumn = 1, 
scale="globalminmax",
showPoints = TRUE, 
title = "Ranking",
alphaLines = 0.3
) + scale_color_viridis(discrete=TRUE) + theme_ipsum()+ theme_void()

但我不能让它看起来像这样:

【问题讨论】:

    标签: r graph compare ranking ggally


    【解决方案1】:

    如果我正确理解“对称”的含义:如果两列中的 Rankings 不匹配,您将无法重现这样的图表。在Ranking1 中有c(1:10),在Ranking2 中有c(1:7, 9, 11, 16)

    这是一个更接近目标的最小示例:

    数据

    # Data with corrected rankings (1:10)
    data <- read.table(text="
    Group    Ranking1    Ranking2     Pop
      a           1            1      12345
      b           2            4      127868
      c           3            2      123477
      d           4            3      9485
      e           5            7      132588
      f           6            5      38741
      g           7            9      8372
      h           8            8     53423
      i           9            6      238419
      j           10           10     31314
               
               ", header = TRUE)
    

    代码

    # Build plot
    GGally::ggparcoord(data,
                       columns = 2:3, groupColumn = 1,  
                       scale="globalminmax", 
                       showPoints = TRUE, 
                       title = "Ranking"
    ) + 
        # Reversed y axis with custom breaks to recreate 1:10 rankings
        scale_y_reverse(breaks = 1:10)
    

    编辑:让它漂亮

    如果您想添加一些比萨饼(就像您尝试做的那样),您可以执行以下操作(无需使用theme_void()):

    GGally::ggparcoord(data,
                       columns = 2:3, groupColumn = 1,  
                       scale="globalminmax", 
                       showPoints = TRUE, 
                       title = "Ranking"
    ) + 
        # Reverses scale, adds pretty breaks
        scale_y_reverse(breaks = 1:10) + 
        # Prettifies typography etc.
        hrbrthemes::theme_ipsum() + 
        # Removes gridlines
        theme(
            panel.grid.major = element_blank(), 
            panel.grid.minor = element_blank()
        ) + 
        # Removes axis labels
        labs(
            y = element_blank(), 
            x = element_blank()
        )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-21
      • 1970-01-01
      • 1970-01-01
      • 2017-08-10
      • 1970-01-01
      相关资源
      最近更新 更多