【问题标题】:ordering facet_wrap levels by frequency按频率排序 facet_wrap 级别
【发布时间】:2021-09-08 16:46:47
【问题描述】:

我正在尝试为迄今为止 F1 车手赢得的冠军制作华夫饼图。图表很好,但它带有字母标签。我希望它从赢得的冠军头衔最多开始到最少。

我尝试了 orderingfct_relevel。但没有任何效果。下面是代码

ggplot(data = dfc, aes(fill=Champions, values=one)) +
  geom_waffle(color = "cornsilk", size=0.25, n_rows=7)+
  facet_wrap(~Champions, nrow = 3, strip.position = "bottom",labeller = label_wrap_gen(6))

这就是 result我在找。

你可以找到完整的代码here

数据集看起来像

Season    Champions    Team   one
1              a           x     1
2              a           x     1
3              b           y     1
4              a           x     1
5              c           z     1

【问题讨论】:

  • Error: At least one layer must contain all faceting variables: `Champions`。 (它不在您的示例数据中。)
  • @r2evans 对不起,我的错。刚刚编辑过
  • 请修剪您的代码,以便更容易找到您的问题。请按照以下指南创建minimal reproducible example
  • 您是否尝试过使用fct_reorder
  • @RichardTelford 是的,也试过了。没用,但答案有效(fct_infreq)

标签: r ggplot2 visualization facet-wrap waffle-chart


【解决方案1】:

这是使用forcats(也是tidyverse 包的一部分)的解决方案。

fct_infreq() 根据它们在数据中的频率对因子进行排序,您可以使用它来指定数据中水平的排序。

dfc$Champions <- factor(dfc$Champions, levels=levels(fct_infreq(dfc$Champions)))

ggplot(data = dfc, aes(fill=Champions, values=one)) +
  geom_waffle(color = "cornsilk", size=0.25, n_rows=7) +
  facet_wrap(~Champions, nrow = 3, strip.position = "bottom", labeller = label_wrap_gen(6))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-30
    • 2014-11-07
    • 1970-01-01
    • 1970-01-01
    • 2016-08-27
    • 1970-01-01
    • 2020-02-06
    相关资源
    最近更新 更多