ggplot在facet_wrap中重命名facet标签

19

我在编写ggplot函数时遇到了障碍。我正在尝试更改ggplot facet_wrap图中的分面标签,但这比我想象的要棘手...

我使用的数据可以在此处访问

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

这个方法是可行的,但现在我遇到的问题是,我想要标签中的数字上标。有没有人能建议最好的方法来实现这个目标呢? 谢谢。


我认为在ggplot的开发版本中,"labeller"参数可以用于facet_wrap函数。你可以在这个链接中找到更多信息:http://docs.ggplot2.org/dev/facet_wrap.html - Ben Bolker
@BenBolker 谢谢...那会非常有用 - 我该如何安装它? - Rory Shaw
@eipi10 我认为 labeller 不能传递给 facet_wrap... 这是问题的关键所在。 - Rory Shaw
我已经更新了我的答案,使用了facet_wrap,它可以正常工作,但是我正在使用ggplot的开发版本。 - eipi10
facet_grid 相关的流行问题 https://dev59.com/7nA75IYBdhLWcg3wGlAa - IloveCatRPython
2个回答

15

设置面板标签为适当的表达式,然后使用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)

如果你不在^14*C前添加NULL,由于^作为初始字符,会导致错误。根据你是否需要每个表达式部分之间有空格,*~标记每个部分的边界。

截至本篇文章发布时(2015年12月12日),你需要使用ggplot2的开发版本才能使其与facet_wrap一起使用。然而,这个特性很可能很快就会被纳入包的正式发布版本中。

图片描述


2
使用“NULL”的巧妙技巧可以启用初始上标。但OP的问题是关于facet_wrap,而不是facet_grid - Matt74
2
我已更新为使用 facet_wrap,也起作用了。我正在使用 ggplot 的开发版本 (ggplot2_1.0.1.9003)。也许它在最新的 CRAN 发布版中无法工作? - eipi10
@eipi10 太棒了...我怎样才能获取开发版本? - Rory Shaw
2
如果你没有 devtools 包,那么请安装并加载它。接着执行 install_github("hadley/ggplot2") - eipi10
你是在尝试安装 ggplot2 的 dev 版本时遇到了这个问题吗? - eipi10
显示剩余5条评论

4

成功解决了!在安装 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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接