ggplot 重命名facet_wrap 中的构面标签

问题描述 投票:0回答:3

我在编写 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)}

产生了这个可爱的图表:

这很好,但我现在想更改方面标签...... 在谷歌上进行了一番探索后,我想我也许可以使用

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

这可行,但我现在遇到的问题是我希望标签中的数字带有上标。谁能建议实现这一目标的最佳方法? 谢谢

r ggplot2 facet-wrap
3个回答
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)

您需要在

NULL
之前添加
^14*C
,否则会因
^
作为初始字符而出现错误。
*
~
标记表达式每个部分的边界,具体取决于您是否希望每个部分之间有空格。

截至撰写本文时(2015 年 12 月 12 日),您需要使用

ggplot2
的开发版本才能与
facet_wrap
配合使用。然而,此功能可能很快就会被纳入该软件包的常规版本中。


5
投票

设法解决它! 安装

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

然后我调整了 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
会给出下面带有正确的方面标签的图表。


0
投票

我想你也可以使用更简单的东西,例如:

facet_wrap(factor(Species, levels = c("A", "B", "C"), labels = c("a", "b", "c")) ~.)

© www.soinside.com 2019 - 2024. All rights reserved.