4

我有一个数据集,我将其作为 CSV 格式的要点上传到此处。它是 YouGov 文章“‘好’有多好?”中提供的 PDF 的提取形式。. 被要求以 0(非常负面)和 10(非常正面)之间的分数对单词(例如“完美”、“糟糕”)进行评分的人。gist 包含准确的数据,即对于每个单词(列:Word),它存储从 0 到 10(列:类别)的每个排名(列:类别)的投票数(列:总计)。

由于我缺乏 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. 或者按类别值对脊进行着色,即使用不同的颜色。

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

我希望最终得到一个更接近这个情节的结果,它满足标准 3(来源): 在此处输入图像描述

4

2 回答 2

5

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

> 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 也会相应地订购颜色,因此我们不必修改自己,除非您更喜欢不同的调色板。

于 2020-01-16T13:46:08.127 回答
2

另一个解决方案是完全正确的。我只是想指出,您可以fct_reorder()从内部调用aes()以获得更紧凑的解决方案。但是,如果要沿 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 包(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 包(v0.3.0)于 2020-01-19 创建

于 2020-01-20T05:31:44.607 回答