Yohan Obadia Yohan Obadia - 2 months ago 7
R Question

Display facet names on ggplot2

I can't figure out why the following code do not display any name for the facets of my plots. If anyone as a hint that would be welcome.

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


When I run the code:

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


I get:



The first facet should be named Large choice here for instance.
I must also mention that the data table is obtained via a user input so I do not know before hand what id will look like though I know they will be strings.

I have tried to apply levels to id because there was noone, however it did not work and I tried to use the labeller function, but nothing either.

> 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

Answer

Ok, I found where the problem was. Obviously with a partial code you could not find it...

We had a piece of code somewhere in another file which was called before stating:

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

Since the default is switch = "x" in facet_wrap, it could not work. When I do the following however, it works fine:

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