1
votes

I am trying to change the facet labels in ggplot2. I found this solution

but it is not working for me. Here is the code I am using

facets <- c("1", "2", "3", "4", "5")

names <- list(
  '1'="one",
  '2'="two",
  '3'="three",
  '4'="four",
  '5'="five"
)

name_labeller <- function(variable,value){
  return(names[value])
}

ggplot(Data[Data$Names %in% facets,], aes(y = y, x = x, group = Names)) + 
  geom_point(shape = 21, size=3, aes(fill=Part)) + 
  scale_fill_manual(values=c("gray90","gray40")) + 
  geom_smooth(method="lm", se= FALSE, size = 1, aes(color = Part, group = Part)) +
  scale_color_manual(values=c("black","black")) +
  geom_smooth(method = 'lm', size = 1, colour = 'red', se = FALSE)  +
  scale_x_log10() + 
  scale_y_log10() + 
  theme_bw() +
  facet_grid(Names ~ ., labeller=name_labeller)

When I run this, the facets on the plot read "one" "three" "four" "[empty]" and "[empty]" (i.e. the last two facets are not labelled), instead of "one" "two" "three" "four" and "five". Also, it's assigning "three" to facet 2 and "four" to facet three.

Note: If I change the order of the items in the list "names", this affects the facets that are labelled. It seems there is an issue with the name_labeller not returning the correct word, OR labeller-name_labeller isn't asking for the correct thing?

This solution linked above is from 2010, perhaps ggplot2 has changed since then? Does anyone know how I can change the facet labels on this plot?

Many thanks!

EDIT: Also, I would like to display the text in italics if possible, e.g. "one" "two" "three" "four" "five". I was going to tackle that once I could actually change the facets, but if anyone knows offhand, I'd appreciate it!

1
Why you have facet_grid() and facet_wrap() in one plot?? You should use only one of them.Didzis Elferts
Also it is hard to help in your particular case because we don't have your Data and can't test your code.Didzis Elferts
Thanks for your response Didzis! Thanks for point out the problem with having facet_grid() and facet_wrap() in the code. I removed facet_wrap(), and now three of the labels are showing up, but in the wrong facet, e.g. it's showing "one" "three" and "four" on the top 3 facets. I will edit my original post to reflect this.Thomas
As Didzis explained, we really can't help much without being able to run code that generates the behavior you describe. You need to create a small reproducible example.joran
Thanks joran! Alas, the above code is working PERFECTLY for me in my small reproducible example... Argh! Maybe there is some problem with my input data, I will play around and update if I can figure out what i'm doing wrong.Thomas

1 Answers

0
votes

OK I have discovered the problem. I have a grouping variable in my dataframe called Part, and this has empty values for some of the other data that were not included in my plot (these are other values for Data$Names that were not included in my list called facets). For some reason, even though I was not plotting these data, the empty values were interfering with the faceting. I filled in these empty values in my data, and the above code now works perfectly.

Thanks to Didzis and joran for their comments, which pushed me to keep working until I got an answer.