Temperature symbols in facet labels

561 Views Asked by At

I would like to have a facet plot, the labels should be "13℃","20℃","27℃". I tried to make it in data set, sometimes it works, but not always. Is there some other solutions? Thank you for any comments. facet plot

1

There are 1 best solutions below

8
On

You could do:

Example data:

    df=structure(list(variable = c(13L, 13L, 13L, 13L, 14L, 14L, 14L, 
14L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 16L, 17L, 17L, 17L, 17L, 
18L, 18L, 18L, 18L, 19L, 19L, 19L, 19L), value = c(480L, 720L, 
460L, 220L, 780L, 350L, 480L, 240L, 431L, 377L, 179L, 876L, 295L, 
255L, 560L, 789L, 670L, 340L, 60L, 820L, 360L, 615L, 735L, 100L, 
190L, 345L, 1260L, 75L), grp = c("A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "B", "B", "B", "B")), .Names = c("variable", 
"value", "grp"), row.names = c(NA, -28L), class = "data.frame")

library(ggplot2)
ggplot(df,aes(value)) +
  geom_density() +
  facet_grid(grp ~variable,labeller =labeller(.cols = function(string) paste(string, "°C")))

or

ggplot(df,aes(value)) +
  geom_density() +
  facet_grid(grp ~variable,labeller =labeller(variable = function(string) paste(string, "°C")))

enter image description here Edit: as per aosmith's comment I changed the function to something more compact that impacts only 1 variable