facet_wrap面板未按facet_wrap变量的级别顺序显示

时间:2019-01-21 07:14:14

标签: r sequence facet levels

我观察到一个奇怪的行为,我肯定与向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 =宝宝的名字

  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 <-factor(data.babyname.all $ name,level = 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) 

这是输出的样子。随意注释出绘图的最后一行。  the output of ggplot

3 个答案:

答案 0 :(得分:0)

请原谅我没有安装软件包并测试整个过程,但是一开始就是这样:您不是应该明确声明订购它们吗?像这样:

data.babyname.all$name <- factor(data.babyname.all$name , levels = list.top.35.names, ordered = TRUE)

答案 1 :(得分:0)

没有geom_point的相同代码(例如@parpar指出)工作得很好

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

data.babyname.all <- babynames::babynames

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

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.babyname.all  %>%
  filter(name %in% list.top.35.names) %>%
  group_by(name,year) %>% 
  mutate(perc= n/sum(n)) %>%
  ungroup()


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

data.babynames.total %>% 
  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'))  +  
  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')) 

enter image description here

答案 2 :(得分:0)

我知道了。 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)