R在数据框中创建希腊符号

时间:2016-12-08 01:24:57

标签: r ggplot2 symbols

我正在尝试创建一个包含希腊符号的自定义构面标题列,用于ggplot。但是,由于某种原因,函数expression()无法正常工作。

如果我使用

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

导致

> 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

如果我使用

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

它给了我

> 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

如您所见,两种情况都没有在数据框中给我一个希腊符号。 我也使用bquote()无济于事。名为fac的列应该基本上存储构面标签。例如,对于第1小时,格式应为12 - 1 am; μt= 3721141您能告诉我正确的方法吗?

以下是示例数据

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

1 个答案:

答案 0 :(得分:2)

不要尝试在数据框中放置表达式 - 您可以包含要解析的文本向量,然后使用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