ggplot在facet_wrap中重命名facet标签

ggplot在facet_wrap中重命名facet标签,r,ggplot2,facet-wrap,R,Ggplot2,Facet Wrap,我在编写ggplot函数时遇到了一个绊脚石。我正在尝试更改ggplot facet\u wrap plot中的facet标签。。。。但事实证明,这比我想象的要复杂 我使用的数据可以在这里访问 str(ggdata) 'data.frame': 72 obs. of 8 variables: $ Season : Factor w/ 3 levels "Autumn","Spring",..: 2 2 2 2 2 2 2 2 2 2 ... $ S

我在编写ggplot函数时遇到了一个绊脚石。我正在尝试更改ggplot facet\u wrap plot中的facet标签。。。。但事实证明,这比我想象的要复杂

我使用的数据可以在这里访问

str(ggdata)
'data.frame':   72 obs. of  8 variables:
 $ Season : Factor w/ 3 levels "Autumn","Spring",..: 2 2 2 2 2 2 2 2 2 2 ...
 $ Site   : Factor w/ 27 levels "Afon Cadnant",..: 13 13 13 13 13 13 13 13 13 13 ...
 $ Isotope: Factor w/ 4 levels "14CAA","14CGlu",..: 1 1 1 1 1 1 2 2 2 2 ...
 $ Time   : int  0 2 5 24 48 72 0 2 5 24 ...
 $ n      : int  3 3 3 3 3 3 3 3 3 3 ...
 $ mean   : num  100 88.4 80.7 40.5 27.6 ...
 $ sd     : num  0 1.74 2.85 2.58 2.55 ...
 $ se     : num  0 1 1.65 1.49 1.47 ...
我编写了以下函数来创建ggplot,它使用同位素因子级别来标记刻面:

plot_func <- function(T) {site_plots <- ggplot(data = T) + geom_point(aes(Time, mean, colour = Season, shape = Season)) + 
  geom_line(aes(Time, mean, colour = Season, linetype = Season)) +
  geom_errorbar(aes(Time, mean, ymax = (mean + se), ymin = (mean - se)), width = 2) +
  labs(title = T$Site[1], y = "Percentage of isotope remaining in solution", x = "Time (h)") +
  scale_x_continuous(breaks=c(0, 24, 48, 72)) +
  scale_y_continuous(limits=c(0,115), breaks = c(0,25,50,75,100)) +  
  theme(axis.title.y = element_text(vjust = 5)) +
  theme(axis.title.x = element_text(vjust = -5)) + theme(plot.title =  element_text(vjust = -10)) +
  theme_bw() + facet_wrap(~Isotope, ncol =2) 
  print(site_plots)
  ggsave(plot = site_plots, filename = paste(T$Site[1], ".pdf"), 
     path = "C:/Users/afs61d/Dropbox/Academic/R/Practice datasets/Helens_data/Site_Isotope_Season_plots/", 
     width = 9, height = 7, dpi = 300)}
这是可行的,但我现在的问题是,我希望标签中的数字是超级脚本。有人能提出实现这一目标的最佳方法吗?
谢谢

将刻面标签设置为适当的表达式,然后使用
标签机
函数
标签解析
确保正确显示。下面是一个示例,使用内置的
iris
数据帧:

data(iris)
iris$Species = as.character(iris$Species)
iris$Species[iris$Species == "virginica"] = "NULL^14*C~Amino~Acids"

ggplot(iris, aes(Sepal.Width, Sepal.Length)) +
  geom_point() +
  facet_wrap(~ Species, labeller=label_parsed)
您需要在
^14*C
之前添加
NULL
,否则会因为将
^
作为初始字符而出现错误<代码>*和
~
标记表达式每个部分的边界,具体取决于您是否希望在每个部分之间留有空格

在撰写本文(2015年12月12日)时,您需要使用
ggplot2
的开发版本才能使用
facet\u wrap
。然而,这一特性可能很快就会被纳入到包的常规版本中

设法解决它! 在安装
ggplot
的开发版本时遇到问题,但在安装
curl
devtools
并重新安装
scales
后,它工作了。我尝试了@eipi10答案,但无法实现,因此我以不同的方式更改了因子标签名称:

ggdata$Isotope <- factor(ggdata$Isotope, labels = c("NULL^14*C~Amino~Acids", 
"NULL^14*C~Glucose", "NULL^14*C~Glucose-6-phosphate", "NULL^33*P~Phosphate"))
通过将
ggdata
传递到
plot_func
可以得到以下带有正确刻面标签的图表


我认为在ggplot的开发版本中,facet_wrap可以使用“labeller”:@BenBolker谢谢。。。这将非常有用-我如何安装它?@eipi10我不认为
labeller
可以传递到
facet\u wrap
。。。这就是问题的症结所在。我已经更新了我的答案,使用了
facet\u wrap
,这是可行的,但我使用的是ggplot.related流行问题的开发版本,但是对于
facet\u grid
使用“NULL”的巧妙技巧来启用初始上标。但是OP的问题是关于
facet\u-wrap
,而不是
facet\u-grid
@eipi10谢谢,但是这对
facet\u-wrap
不起作用。。。虽然我已更新为使用
facet\u wrap
,但查看解析的
标签
仍很有用。我使用的是ggplot的开发版本(
ggplot2_1.0.1.9003
)。也许它在最新的CRAN版本中不起作用?@eipi10太好了。。。如何获得开发版本?如果您没有
devtools
软件包,请安装并加载它。然后安装github(“hadley/ggplot2”)。
ggdata$Isotope <- factor(ggdata$Isotope, labels = c("NULL^14*C~Amino~Acids", 
"NULL^14*C~Glucose", "NULL^14*C~Glucose-6-phosphate", "NULL^33*P~Phosphate"))
plot_func <- function(T) {site_plots <- ggplot(data = T) + geom_point(aes(Time, mean, colour = Season, shape = Season)) + 
  geom_line(aes(Time, mean, colour = Season, linetype = Season)) +
  geom_errorbar(aes(Time, mean, ymax = (mean + se), ymin = (mean - se)), width = 2) +
  labs(title = T$Site[1], y = "Percentage of isotope remaining in solution", x = "Time (h)") +
  scale_x_continuous(breaks=c(0, 24, 48, 72)) +
  scale_y_continuous(limits=c(0,115), breaks = c(0,25,50,75,100)) +  
  theme(axis.title.y = element_text(vjust = 5)) +
  theme(axis.title.x = element_text(vjust = -5)) + theme(plot.title = element_text(vjust = -10)) +
  theme_bw() + facet_wrap(~Isotope, ncol =2, labeller = label_parsed) 
  print(site_plots)
  ggsave(plot = site_plots, filename = paste(T$Site[1], ".pdf"), 
     path = "C:/Users/afs61d/Dropbox/Academic/R/Practice datasets/Helens_data/Site_Isotope_Season_plots/", 
     width = 9, height = 7, dpi = 300)}