在 ggplot2 上显示构面名称

Display facet names on ggplot2

我不明白为什么下面的代码没有为我的图的各个方面显示任何名称。如果有人作为提示,欢迎。

data <- data.frame(id = rep(c("Large choice", "Low prices", "Service quality", "Product quality", "Convenience"), 4), 
                   variable = rep(factor(c("OfficeStar", "Paper and Co", "Office Equipment", "Supermarket"), 
                   levels = c("OfficeStar", "Paper and Co", "Office Equipment", "Supermarket")), each = 5), 
                   value = sample(30, 20), 
                   focus = as.factor(c(rep(1,5), rep(0, 15)))); 
nb_col <- 3

> head(data, 10)
       id             variable       value   focus
       Large choice   OfficeStar     5.2     1
         Low prices   OfficeStar     2.1     1
    Service quality   OfficeStar     4.2     1
    Product quality   OfficeStar     3.7     1
        Convenience   OfficeStar     2.7     1
       Large choice   Paper and Co   4.4     0
         Low prices   Paper and Co   4.5     0
    Service quality   Paper and Co   2.3     0
    Product quality   Paper and Co   2.6     0
        Convenience   Paper and Co   1.4     0

当我运行代码:

ggplot(data = data, aes(x = variable, y = value, fill = focus)) +
     geom_bar(stat = "identity") +
     facet_wrap(~id, ncol = nb_col, drop = FALSE) +
     scale_fill_manual(values = c("#77bb77", "#5599bb")) +
     coord_flip() +
     theme(axis.title.x = element_blank(),
           axis.title.y = element_blank(),
           panel.grid   = element_blank(),
           legend.position = "none")

我得到:

这里的第一个方面应该命名为Large choice。 我还必须提到数据 table 是通过用户输入获得的,所以我事先不知道 id 会是什么样子,尽管我知道它们将是字符串。

我曾尝试将级别应用于 id,因为没有人,但是它不起作用,我尝试使用标签功能,但也没有。

> sessionInfo()
R version 3.3.1 (2016-06-21)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] gridExtra_2.2.1 ggrepel_0.5     GMD_0.3.3       cluster_2.0.4   proto_0.3-10    lme4_1.1-12     Matrix_1.2-6    MASS_7.3-45     nnet_7.3-12    
[10] sendmailR_1.2-1 RODBC_1.3-13    scales_0.4.0    ggplot2_2.1.0   gplots_3.0.1    stringr_1.1.0   digest_0.6.10   reshape2_1.4.1 

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.6        magrittr_1.5       splines_3.3.1      munsell_0.4.3      lattice_0.20-33    colorspace_1.2-6   minqa_1.2.4        plyr_1.8.4        
 [9] caTools_1.17.1     tools_3.3.1        nlme_3.1-128       gtable_0.2.0       KernSmooth_2.23-15 gtools_3.5.0       nloptr_1.0.4       base64enc_0.1-3   
[17] bitops_1.0-6       labeling_0.3       gdata_2.17.0       stringi_1.1.1     

好的,我找到问题所在了。显然使用部分代码你找不到它...

我们在另一个文件的某处有一段代码,在声明之前被调用:

theme_update(strip.text.x = element_blank())

由于在facet_wrap中默认为switch = "x",所以无法正常工作。但是,当我执行以下操作时,它工作正常:

ggplot(data = data, aes(x = variable, y = value, fill = focus)) +
       geom_bar(stat = "identity") +
       facet_wrap(~id, ncol = 3, drop = FALSE, labeller = "label_value", switch = "y") +
       coord_flip() +
       theme(axis.title.x = element_blank(),
             axis.title.y = element_blank(),
             panel.grid   = element_blank(),
             legend.position = "none")