簡體   English   中英

ggplot在facet_wrap中重命名facet標簽

[英]ggplot renaming facet labels in facet_wrap

我在編寫ggplot函數時遇到了絆腳石。 我正在嘗試更改ggplot facet_wrap圖中的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,它使用Isotope因子級別來標注facet:

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

這有效,但我現在遇到的問題是我希望標簽中的數字是超級腳本的。 誰能建議最好的方法來達到這個目的? 謝謝

設置小標簽,以適當的表情,然后使用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 但是,這個功能可能會很快被納入包的常規版本中。

在此輸入圖像描述

管理以解決它! 假如安裝的開發版本麻煩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會給出下面帶有正確facet標簽的圖形。

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM