【问题标题】:Ridge plot: sort by value / rank岭图:按值/等级排序
【发布时间】:2020-05-03 07:11:18
【问题描述】:

我有一个数据集,我上传了 here 作为 CSV 格式的要点。 它是 YouGov 文章 "How good is 'good'?" 中提供的 PDF 的提取形式。被要求对分数在 0(非常负面)和 10(非常正面)之间的单词(例如“完美”、“糟糕”)进行评分的人。 gist 正好包含该数据,即它为每个单词(列:Word)存储从 0 到 10(列:Category)的每个排名(列:Category)的投票数(列:Total)。

由于我缺乏 R 知识,我通常会尝试使用 matplotlib 和 Python 可视化数据,但似乎 ggridges 可以创建比我自己使用 Python 做的更好的图。

使用:

library(ggplot2)
library(ggridges)

YouGov <- read_csv("https://gist.githubusercontent.com/camminady/2e3aeab04fc3f5d3023ffc17860f0ba4/raw/97161888935c52407b0a377ebc932cc0c1490069/poll.csv")

ggplot(YouGov, aes(x=Category, y=Word, height = Total, group = Word, fill=Word)) + 
  geom_density_ridges(stat = "identity", scale = 3)

我能够创建这个情节(仍然远非完美):

忽略我必须调整美学的事实,我很难做三件事:

  1. 按平均排名对单词进行排序。
  2. 按平均等级为山脊着色。
  3. 或按类别值对脊进行着色,即使用不同的颜色。

我尝试调整来自 this source 的建议,但最终失败了,因为我的数据似乎格式错误:我已经拥有每个类别的汇总投票数,而不是单个投票实例。

我希望最终得到一个更接近这个情节的结果,它满足标准 3 (source):

【问题讨论】:

    标签: r ggplot2 data-visualization ridgeline-plot


    【解决方案1】:

    我自己花了一点时间才到达那里。对我来说,理解数据以及如何根据平均 Category 分数订购 Word 的关键。那么我们先来看数据:

    > YouGov
    # A tibble: 440 x 17
          ID Word  Category Total  Male Female `18 to 35` `35 to 54` `55+`
       <dbl> <chr>    <dbl> <dbl> <dbl>  <dbl>      <dbl>      <dbl> <dbl>
     1     0 Incr~        0     0     0      0          0          0     0
     2     1 Incr~        1     1     1      1          1          1     0
     3     2 Incr~        2     0     0      0          0          0     0
     4     3 Incr~        3     1     1      1          1          1     1
     5     4 Incr~        4     1     1      1          1          1     1
     6     5 Incr~        5     5     6      5          6          5     5
     7     6 Incr~        6     6     7      5          5          8     5
     8     7 Incr~        7     9    10      8         10          7    10
     9     8 Incr~        8    15    16     14         13         15    16
    10     9 Incr~        9    20    20     20         22         18    19
    # ... with 430 more rows, and 8 more variables: Northeast <dbl>,
    #   Midwest <dbl>, South <dbl>, West <dbl>, White <dbl>, Black <dbl>,
    #   Hispanic <dbl>, `Other (NET)` <dbl>
    

    每个单词对于每个类别(或分数,1-10)都有一行。 Total 提供了针对该 Word/Category 组合的响应数。因此,尽管“难以置信”这个词的得分为零,但没有任何回应,但仍然有一行。

    在计算每个单词的平均分数之前,我们会计算每个单词-类别组合的类别和总分的乘积,我们称之为总分。从那里,我们可以将Word 视为一个因素,并使用forcats 根据平均总分重新排序。之后,您可以像以前一样绘制数据。

    library(tidyverse)
    library(ggridges)
    
    YouGov <- read_csv("https://gist.githubusercontent.com/camminady/2e3aeab04fc3f5d3023ffc17860f0ba4/raw/97161888935c52407b0a377ebc932cc0c1490069/poll.csv")
    
    YouGov %>% 
      mutate(total_score = Category*Total) %>% 
      mutate(Word = fct_reorder(.f = Word, .x = total_score, .fun = mean)) %>% 
      ggplot(aes(x=Category, y=Word, height = Total, group = Word, fill=Word)) + 
      geom_density_ridges(stat = "identity", scale = 3)
    

    通过将单词视为一个因素,我们根据单词的平均类别对单词进行了重新排序。 ggplot 也会相应地订购颜色,因此我们不必修改自己,除非您更喜欢不同的调色板。

    【讨论】:

    • @k1next 没问题,很高兴它有帮助!
    【解决方案2】:

    另一个解决方案是完全正确的。我只是想指出,您可以从aes() 中调用fct_reorder(),以获得更紧凑的解决方案。但是,如果要沿 y 轴按位置更改填充颜色,则需要执行两次。

    library(tidyverse)
    library(ggridges)
    
    YouGov <- read_csv("https://gist.githubusercontent.com/camminady/2e3aeab04fc3f5d3023ffc17860f0ba4/raw/97161888935c52407b0a377ebc932cc0c1490069/poll.csv")
    
    ggplot(YouGov,
      aes(
        x = Category,
        y = fct_reorder(Word, Category*Total, .fun = sum),
        height = Total,
        fill = fct_reorder(Word, Category*Total, .fun = sum)
      )) + 
      geom_density_ridges(stat = "identity", scale = 3) +
      theme(legend.position = "none")
    

    reprex package (v0.3.0) 于 2020-01-19 创建

    如果您想按 x 位置着色,则可以执行以下操作。它只是看起来不像温度示例那么好,因为 x 值是离散的。

    library(tidyverse)
    library(ggridges)
    
    YouGov <- read_csv("https://gist.githubusercontent.com/camminady/2e3aeab04fc3f5d3023ffc17860f0ba4/raw/97161888935c52407b0a377ebc932cc0c1490069/poll.csv")
    
    ggplot(YouGov,
      aes(
        x = Category,
        y = fct_reorder(Word, Category*Total, .fun = sum),
        height = Total,
        fill = stat(x)
      )) + 
      geom_density_ridges_gradient(stat = "identity", scale = 3) +
      theme(legend.position = "none") +
      scale_fill_viridis_c(option = "C")
    

    reprex package (v0.3.0) 于 2020-01-19 创建

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-27
      • 2019-03-02
      • 2018-12-12
      • 2017-03-16
      • 2013-06-29
      • 2023-03-17
      相关资源
      最近更新 更多