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!