4

I am trying to create a custom facet title column which contains greek symbols, to be used in ggplot. But, for some reason the function expression() is not working as I expected.

If I use

df$fac <- ifelse(df$hour==1,expression(paste(df$hour+11, " - ", df$hour, " am; ", mu, 't=', round(df$exp), sep="")), "k")

it results in

> tail(df,7)
   hour     exp duration                                                               fac
18    1 3721141 921184.3            expression(paste(df$hour + 11, " - ", df$hour, " am; ", 
19    2 2175734 921844.5            mu, "t=", round(df$exp), sep = ""), "k", "k", "k", "k", 
20   24 6656036 919250.4                                                          "k", "k")
21    6 7869344 918690.6                                                               <NA>
22    4 2220189 920862.5                                                               <NA>
23    5 2780161 920005.2                                                               <NA>
24    3 2690003 922332.2                                                               <NA>
Warning message:
In format.data.frame(x, digits = digits, na.encode = FALSE) :
  corrupt data frame: columns will be truncated or padded with NAs

and if I use

df$fac <- ifelse(df$hour==1,paste(df$hour+11, " - ", df$hour, " am; ",expression(mu), 't=', round(df$exp), sep=""), "k")

it gives me

> tail(df,7)
   hour     exp duration                    fac
18    1 3721141 921184.3 12 - 1 am; mut=3721141
19    2 2175734 921844.5                      k
20   24 6656036 919250.4                      k
21    6 7869344 918690.6                      k
22    4 2220189 920862.5                      k
23    5 2780161 920005.2                      k
24    3 2690003 922332.2                      k

As you can see, both cases do not give me a greek symbol in the dataframe. I also used bquote() to no avail. The column named fac should essentially store the facet label. As an example, for hour 1, the format should be 12 - 1 am; μt= 3721141 Can you please let me know the right approach here?

Here is the sample data

> dput(df)
structure(list(hour = c(14L, 16L, 17L, 19L, 21L, 11L, 18L, 22L, 
8L, 12L, 23L, 9L, 15L, 13L, 7L, 10L, 20L, 1L, 2L, 24L, 6L, 4L, 
5L, 3L), exp = c(3923432.39149989, 5109851.42312691, 7070493.23531211, 
20803629.8380627, 11256055.2295972, 4757385.54314779, 27576092.3667234, 
9141934.32914581, 35348337.463401, 4108302.35295023, 6489550.8228499, 
9848278.71615835, 3816330.82689788, 3790223.01364096, 26886283.3156086, 
6308698.90957706, 17434998.9294133, 3721141.03502774, 2175733.94348253, 
6656035.63316213, 7869343.75835358, 2220189.45505708, 2780160.84185653, 
2690003.082563), duration = c(861081.7025, 865706.1025, 872852.945833333, 
892462.086388889, 908292.911111111, 862927.383055556, 882474.983055556, 
913184.621388889, 895447.985277778, 861349.923888889, 916578.3325, 
878670.013611111, 862413.049722222, 861035.062777778, 911718.763055556, 
868130.278333333, 901928.8125, 921184.328055556, 921844.501111111, 
919250.422777778, 918690.569722222, 920862.491111111, 920005.158333333, 
922332.197777778)), .Names = c("hour", "exp", "duration"), row.names = c(NA, 
-24L), class = "data.frame")
jbaums
  • 27,115
  • 5
  • 79
  • 119
dataanalyst
  • 316
  • 3
  • 12
  • 1
    Don't store expressions in a data.frame. Just set your facet labels when you call ggplot. It would help if you provided a more [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and describe in words what you want in the final plot. – MrFlick Dec 08 '16 at 02:04
  • I have added the sample data with some additional information on the format I need. I was trying to use this df to create labels for the facets and call them using the labeller function. – dataanalyst Dec 08 '16 at 02:22
  • The `dput` output you provided, followed by the code you give, doesn't lead to the data.frame you show – jbaums Dec 08 '16 at 06:14
  • Both the `df`s I show are a result of using the `ifelse` code posted above them. The`dput` `df` has the raw data. – dataanalyst Dec 08 '16 at 07:04

1 Answers1

3

Don't attempt to put expressions in your data.frame - you can include a vector of text to be parsed, and then use labeller=label_parsed:

library(ggplot2)
d <- data.frame(x=runif(20), y=runif(20), grp=rep(c('mu', 'sigma'), 10))
ggplot(d, aes(x=x, y=y)) +
  geom_point() +
  facet_wrap(~grp, labeller=label_parsed)

enter image description here

jbaums
  • 27,115
  • 5
  • 79
  • 119
  • As you can appreciate, my panel names are comprised of 3 different columns (one of which happens to be a greek symbol). I don't entirely see how I can implement this solution to achieve the final format of my panel names. – dataanalyst Dec 08 '16 at 06:07
  • @Gandalf - using your data, you can do something like this... `df$grp <- ifelse(df$hour==1, paste0(df$hour+11, '-', df$hour, '~"am;"', '~mu*t*', '"="', '*', round(df$exp)), 'k'); ggplot(df, aes(x=hour, y=exp)) + geom_point() + facet_wrap(~grp, labeller=label_parsed)`. It's unclear how you want to plot the data, though... – jbaums Dec 08 '16 at 06:26