ggplot在facet_wrap中重命名facet标签

时间:2015-12-12 16:00:21

标签: r ggplot2 facet-wrap

我在编写ggplot函数时遇到了绊脚石。我正在尝试更改ggplot facet_wrap图中的facet标签....但它的证明比我更难,但它会.... [/ p>

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

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

导致这个可爱的图表:

enter image description here

哪个好,但我现在要更改构面标签...... 在google做了一些讨论之后,我想我可以使用labeller函数作为参数传递给facet_wrap。经过一个令人沮丧的一小时后,我发现这只适用于facet_grid !!! ??? 所以,另一种方法是更改​​因子级别名称,所以给我我想要的方面标签::

 gdata$Isotope <- revalue(x = ggdata$Isotope, 
c("14CAA" = " 14C Amino Acids", "14CGlu" = "14C Glucose", 
  "14cGlu6P" = "14C Glucose-6-phosphate", "33P" = "33P Phosphate"))

这样可行,但我现在遇到的问题是我希望标签中的数字是超级脚本的。谁能建议最好的方法来达到这个目的? 感谢

2 个答案:

答案 0 :(得分:12)

将构面标签设置为适当的表达式,然后使用labeller函数label_parsed确保它们正确显示。以下是使用内置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)

您需要在NULL之前添加^14*C,否则您会因为^作为初始字符而收到错误消息。 *~标记表达式每个部分的边界,具体取决于您是否在每个部分之间或确实需要空格。

在撰写本文时(2015年12月12日),您需要使用ggplot2的开发版本才能与facet_wrap一起使用。但是,这个功能可能会很快被纳入包的常规版本中。

enter image description here

答案 1 :(得分:2)

管理排序! 在安装ggplot的开发版本时遇到问题,但在安装curldevtools并重新安装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"))

然后我调整了ggplot函数以将labeller = label_parsed传递给facet_wrap函数:

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

ggdata传递给plot_func会给我以下带有正确构面标签的图表。

enter image description here