【问题标题】:Arranging factors in increasing order按递增顺序排列因素
【发布时间】:2018-01-03 19:14:50
【问题描述】:

我正在尝试创建两个应按降序显示频率的图。

#preparing the data to resemble actual data
test <- data.frame(HairEyeColor) %>%
  mutate(combi = paste(Hair,Eye)) %>%
  group_by(Sex) %>%
  mutate(prop = Freq / sum(Freq))  %>%
  ungroup() 
test$combi <- factor(test$combi)
freq_test_count <- test %>%
  setorder(Freq)

#creating the plot
freq_test_plot <- freq_test_count %>%
  ggplot(aes(x = reorder(combi,prop),y = prop, label = Freq)) +
  geom_col(show.legend = FALSE) +
  geom_text(check_overlap = TRUE, nudge_y = 0.005, size = 3) + 
  facet_wrap(~Sex, scales = "free") +
  labs(y = "Proportion",
       x = NULL) +
  coord_flip()

当我绘制 freq_test_plot 时,它会显示绘图但 output is not in decreasing order

我不确定我应该怎么做才能看到按频率降序排列的术语。

【问题讨论】:

  • 看起来整体呈递减顺序。你的意思是你想要男性和女性的不同顺序的秤?这听起来可能具有误导性,因为需要仔细观察才能意识到标签的顺序不同。
  • 这个想法是为了表明男性和女性的标签顺序不同。我不确定这里的订单是如何决定的。我不明白为什么“金发蓝”30 在“棕蓝”50 之前。
  • 您的订单忽略了性别。 Blond Blue 的总数为 30 + 64 = 94,因此它领先于 Brown Blue,即 50 + 34 = 84。我认为这张图表很有用,因为它按总人口排序,以及特定性别的条形图在哪里不是为了突出两性之间的差异。您要求的图表使比较性别差异变得更加困难,因为您必须来回切换阅读和比较标签。
  • 例如,这个版本清楚地表明,金发蓝色组合在女性中比男性更常见 - 只要你看图表,它就会跳出来。然而,答案中的图表隐藏了这种洞察力,除非你仔细阅读它们。同样,您问题中的图表显示棕色蓝色比女性更常见,但如果您查看答案中的图表,我会假设标签的顺序相同,并且由于分布看起来大致相同,我会在那里得出结论男性和女性分布之间没有真正的区别。

标签: r ggplot2 facet-wrap


【解决方案1】:

一种解决方法是创建两个不同的图并将它们排列在网格中。但是你应该小心,因为like Gregor mentioned,它肯定会产生误导。

library(grid)
p1 = freq_test_count[freq_test_count$Sex == "Male",] %>%
    ggplot(aes(x = reorder(combi,prop),y = prop, label = Freq)) +
    geom_col(show.legend = FALSE) +
    geom_text(check_overlap = TRUE, nudge_y = 0.005, size = 3) + 
    facet_wrap(~Sex, scales = "free") +
    labs(y = "Proportion",
         x = NULL) +
    coord_flip()

p2 = freq_test_count[freq_test_count$Sex == "Female",] %>%
    ggplot(aes(x = reorder(combi,prop),y = prop, label = Freq)) +
    geom_col(show.legend = FALSE) +
    geom_text(check_overlap = TRUE, nudge_y = 0.005, size = 3) + 
    facet_wrap(~Sex, scales = "free") +
    labs(y = "Proportion",
         x = NULL) +
    coord_flip()

graphics.off()
grid.newpage()
grid.draw(ggarrange(p1, p2, ncol = 2))

【讨论】:

  • 谢谢。我希望我不必添加另一个库,因为代码可能会与其他人共享。但似乎这是唯一的方法。如果没有更好的结果,我会接受这个作为答案。
【解决方案2】:

另一种解决方法是为该因子设置男性和女性的特定水平。在这里,我在男性头发/眼睛标签的前面添加了一个空格 " "。这让您可以定义一个将性别考虑在内的排序:

test <- data.frame(HairEyeColor) %>%
  mutate(combi = paste(Hair,Eye)) %>%
  group_by(Sex) %>%
  mutate(prop = Freq / sum(Freq))  %>%
  ungroup() %>%
  mutate(combi = factor(test$combi),
         sex_combi = factor(paste(ifelse(Sex == "Male", " ", ""), Hair, Eye)),
         sex_combi = reorder(sex_combi, prop))

#creating the plot

ggplot(test, aes(x = sex_combi,y = prop, label = Freq)) +
  geom_col(show.legend = FALSE) +
  geom_text(check_overlap = TRUE, nudge_y = 0.005, size = 3) + 
  facet_wrap(~Sex, scales = "free") +
  labs(y = "Proportion",
       x = NULL) +
  coord_flip()

但正如我在 cmets 中提到的,我认为这是一个误导性的情节。

【讨论】:

  • 谢谢。我会检查在我的实际数据中引入一个主角是否可以接受。
【解决方案3】:

您希望按男性还是女性对值进行排序?

library(tidyverse)

#preparing the data to resemble actual data
test <- data.frame(HairEyeColor) %>%
  mutate(combi = paste(Hair,Eye)) %>%
  group_by(Sex) %>%
  mutate(prop = Freq / sum(Freq))  %>%
  ungroup() 
test$combi <- factor(test$combi)


test$combi<- factor(test$combi, levels = unique(test$combi)[order(test$Freq)],)

#creating the plot

  ggplot(test,aes(x = combi,y = prop, label = Freq))+
  geom_col(show.legend = FALSE)+
  geom_text(check_overlap = TRUE, nudge_y = 0.005, size = 3) + 
  facet_wrap(~Sex, scales = "free")+ 
  labs(y = "Proportion",
   x = NULL) +
  coord_flip()

已更新以包含问题的完整代码。

【讨论】:

  • 这不会为我生成排序条形图。
  • 同意@Gregor。此代码不会为我生成排序条形图。
  • 图表的重点是按男性或女性显示排序,而不是男性和女性。运行代码将显示男性按降序排序。
猜你喜欢
  • 1970-01-01
  • 2022-06-27
  • 2023-01-31
  • 2021-01-16
  • 2016-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多