-5

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

X.Chi
  • 45
  • 8
  • 3
    Man, at least provide a MWE of what you already have to produce the above. – wilx Dec 27 '16 at 17:26
  • For the record, [this is a guide to making a MWE](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) as @wilx noted. – bouncyball Dec 27 '16 at 17:55
  • 1
    @bouncyball Thank you! I checked the link question, I will do it next time. Thank you so much for your kind suggestion! – X.Chi Dec 27 '16 at 18:20

1 Answers1

4

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

Haboryme
  • 4,611
  • 2
  • 18
  • 21
  • 1
    Nice solution. It might be cleaner if you define the `function` _outside_ of the call to `facet_wrap`, though. – bouncyball Dec 27 '16 at 17:38
  • @Habryme Thank you to much! It works well, only a little need to change. If I make the facet with two variables, how can I only add the temperature symbol only on one variable? – X.Chi Dec 27 '16 at 17:40
  • 2
    You may be able to use a simpler function, as well, along with `labeller()`: `facet_wrap(~variable, labeller = labeller(variable = function(string) paste(string, "°C")))` The `labeller` help page has some good examples. – aosmith Dec 27 '16 at 17:41
  • Add [NARROW NO-BREAK SPACE](http://www.fileformat.info/info/unicode/char/202f/index.htm) before the degrees Celsius symbol, please. – wilx Dec 27 '16 at 17:57
  • @wilx Sorry, I didn't get it. Could you please show me the detail? Do you mean just add "NARROW NO-BREAK SPACE" before "°C"? Thank you! – X.Chi Dec 27 '16 at 18:06
  • I edited my answer, it should only change one variable now. Aosmith's solution works great. – Haboryme Dec 27 '16 at 18:07
  • @aosmith Thank you very much! – X.Chi Dec 27 '16 at 18:21
  • @Haboryme Super! Many many thanks! – X.Chi Dec 27 '16 at 18:21