首页 > 解决方案 > facet_wrap 面板不显示在 facet_wrap 变量的级别序列中

问题描述

我正在观察一个奇怪的行为,我确信这与向 ggplot 添加更多内容有关。但是,我无法弄清楚是什么导致了这种行为。

我有一个带有一系列名称的向量。

list.top.35.names 
 [1] "Jessie"    "Marion"    "Jackie"    "Alva"      "Trinidad"  "Ollie"     "Carrol"    "Jody"      "Baby"      "Lavern"    "Cleo"      "Marlo"    
[13] "Kerry"     "Ivory"     "Carey"     "Guadalupe" "Frankie"   "Kris"      "Tommie"    "Lupe"      "Arden"     "Darby"     "Angel"     "Hollis"   
[25] "Gale"      "Sammie"    "Lavon"     "Paris"     "Rosario"   "Alpha"     "Ariel"     "Jamie"     "Layne"     "Michel"    "Dee"  

我根据上面的列表重新排序了我的名字因素。

data.babyname.all$name <- factor(data.babyname.all$name , levels = list.top.35.names )
str(data.babyname.all$name)
 Factor w/ 35 levels "Jessie","Marion",..

我希望面板的顺序基于这些级别顺序。不过,出于某种原因,我的图表面板按字母顺序排列。

背景:

#install.packages("babynames") # run this in case you don't have the `babynames` package installed yet
#install.packages("ggrepel")

  library("babynames")
  library("knitr")
  library("tidyverse")
  library("ggrepel")

我使用的数据集 data.babynames.total = babynames

  list.top.35.names = c( "Jessie" ,   "Marion",    "Jackie" ,   "Alva"    ,  "Trinidad"  ,"Ollie"  ,   "Carrol"  ,  "Jody"  ,    "Baby"  ,    "Lavern"   , "Cleo"    ,  "Marlo"    , "Kerry" ,    "Ivory"  ,   "Carey" ,    "Guadalupe" ,"Frankie" ,  "Kris" ,     "Tommie" ,   "Lupe"   ,   "Arden"   ,  "Darby"  ,   "Angel"   ,  "Hollis"   , "Gale"  ,    "Sammie" ,   "Lavon"  ,   "Paris"  ,   "Rosario"  , "Alpha"   ,  "Ariel"  ,   "Jamie"    , "Layne"  ,   "Michel", "Dee") 

我做了更多的清洁工作。为了争论,这只是一点清洁。

  data.babynames.total = data.babynames.total  %>%
     filter(name %in% list.top.35.names) %>%
      group_by(name,year) %>% 
      mutate(perc= n/sum(n)) %>%
      ungroup()

data.babyname.all$name <- 因子(data.babyname.all$name ,levels = list.top.35.names )

data.babyname.all %>% 
  ggplot( mapping = aes(x = year, y = perc, fill = sex)) +
  geom_density(stat = "identity", position = "stack" , show.legend = F ) +
  facet_wrap(~name, ncol= 7) +
  scale_fill_manual(values = c('#E1AEA1','#9ABACF'))  + 
  geom_point(data = most.unisex.year.and.value, mapping = aes(x =  year, y = perc), 
             size = 3, 
             fill = "white", 
             color = "black", 
             shape = 21) +
  theme_minimal() + #set theme
  theme(
          text = element_text(size = 10),
          axis.title.x = element_blank(),
          axis.title.y = element_blank(),
          panel.grid = element_blank(), 
          panel.border = element_blank(),
          plot.background = element_blank(),
          axis.ticks.x = element_line(color = "black"),
          axis.ticks.length =unit(.2,'cm')  ) + 
  scale_y_continuous(breaks = c(0,.50,1), labels= c("0%", "50%","%100")) +
  scale_x_continuous(breaks = c(1940, 1960, 1980,2000), labels= c('1940', "'60","'80",'2000')) +
 geom_text(mapping = aes(x =x , y = y , label = label),  check_overlap = F, na.rm = T) 

这就是输出的样子。随意注释掉绘图的最后一行。 ggplot 的输出

标签: rsequencefacetlevels

解决方案


我想到了。most.unisex.year.and.value 具有名称、x 和 y 列。它有 35 行。我所要做的就是根据前 35 个名称顺序对名称因子进行排序。

most.unisex.year.and.value$name <- factor(most.unisex.year.and.value$name , levels = list.top.35.names , ordered = TRUE)

推荐阅读