Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Creating greek symbols in a dataframe

Tags:

symbols

r

ggplot2

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")
like image 430
dataanalyst Avatar asked Sep 20 '25 07:09

dataanalyst


1 Answers

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

like image 75
jbaums Avatar answered Sep 21 '25 23:09

jbaums