按顺序排列因素

问题描述 投票:0回答:3

我正在尝试创建两个应该按递减顺序显示频率的图。

#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

我不知道我该怎么办才能看到频率降序的条件。

enter image description here

r ggplot2 facet-wrap
3个回答
2
投票

解决方法是创建两个不同的图并将它们排列在网格中。但你应该谨慎,因为,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))

enter image description here


1
投票

另一种解决方法是使该因子的男性和女性特定水平。在这里,我在男性头发/眼睛标签的正面添加了一个空间" "。这允许您定义将性别考虑在内的排序:

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()

enter image description here

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


0
投票

您是否希望将值分类为男性或女性?

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()

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

© www.soinside.com 2019 - 2024. All rights reserved.