0

There is an example provided here, but I just can't make it work. Here is my use case:

df <- as.data.frame(matrix(runif(9),8,8))
angles <- c(0.112, 2.633, 3.766, 5.687, 0.867, 7.978, 8.937, 4.652)
df$factor <- as.factor(angles)
df.m <- melt(df)
ggplot(df.m, aes(variable, value)) +
  geom_boxplot() +
  facet_wrap(~factor)

enter image description here

Now I want to display rounded angles with the degree symbol. So I tried this

new.labs <- as_labeller(paste(round(as.numeric(angles)), "degree"), label_parsed)
ggplot(df.m, aes(variable, value)) +
  geom_boxplot() +
  facet_wrap(~factor, labeller=new.labs)   

But it produces empty strings.

Community
  • 1
  • 1
Ben
  • 6,321
  • 9
  • 40
  • 76

2 Answers2

1

I believe as.labeller is expecting either a function or a look-up table, not a variable name.

new.labs <- as_labeller(function(string) paste(round(as.numeric(string)), "*degree"), label_parsed)

ggplot(df.m, aes(variable, value)) +
    geom_boxplot() +
    facet_wrap(~factor, labeller = new.labs) 

enter image description here

aosmith
  • 34,856
  • 9
  • 84
  • 118
  • There seem to be a problem with your output, though: it uses the index of the angles, not their values. – Ben May 27 '16 at 16:53
  • @Ben This looks like angles rounded to me... How many digits do you want rounded to? I just rounded to the same amount you did in your question. – aosmith May 27 '16 at 17:58
  • Yes it is... I guess it's week-end time for me :) – Ben May 27 '16 at 18:08
1

Not using as.labeller, but this is simpler:

df <- as.data.frame(matrix(runif(9),8,8))
angles <- c(0.112, 2.633, 3.766, 5.687, 0.867, 7.978, 8.937, 4.652)
df$factor <- as.factor(round(angles))
df.m <- melt(df)

ggplot(df.m, aes(variable, value)) +
    geom_boxplot() +
    facet_wrap(~factor, labeller = label_bquote(.(as.character(factor))*degree))
YvanR
  • 365
  • 2
  • 6