小平面标签中的温度符号

时间:2016-12-27 17:12:47

标签: r ggplot2 facet temperature

我想有一个小平面图,标签应该是“13℃”,“20℃”,“27℃”。我试图在数据集中创建它,有时它可以工作,但并非总是如此。 还有其他解决方案吗? 谢谢你的任何评论。 facet plot

1 个答案:

答案 0 :(得分:4)

你可以这样做:

示例数据:

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

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

enter image description here 编辑:根据aosmith的评论,我将功能改为更紧凑,只影响1个变量