ggplot在facet_wrap中重命名facet标签 [英] ggplot renaming facet labels in facet_wrap

查看:1096
本文介绍了ggplot在facet_wrap中重命名facet标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在编写ggplot函数时遇到了一个绊脚石。我试图改变ggplot facet_wrap情节中的facet标签....但它的证明比我更棘手,尽管它会是....



数据I我可以在这里访问

  str(ggdata)
'data.frame':72 obs。 8个变量:
$季节:因子w / 3个等级秋天,春天,..:2 2 2 2 2 2 2 2 2 2 ...
$网站: 27个等级Afon Cadnant,..:13 13 13 13 13 13 13 13 13 13 ...
$同位素:因子w / 4等级14CAA,14CGlu,..:1 1 1 1 1 1 2 2 2 2 ...
$时间: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: (函数(T){site_plots <-ggplot(data = T)+ geom_point(aes(Time,mean ,颜色=季节,形状=季节))+
geom_line(aes(Time,mean,color = Season,linetype = Season))+
geom_errorbar(aes(Time,mean,ymax = se),ymin =(mean-se)),width = 2)+
labs(title = T $ Site [1],y =解决方案中剩余同位素的百分比,x =时间(h))+
scale_x_continuous(休息= 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 /练习数据集/ Helens_data / Site_Isotope_Season_plots /,
width = 9,height = 7,dpi = 300)}

导致这张可爱的图:



哪个不错,但我现在要更改facet标签...
完成后我认为我可以使用 labeller 函数作为参数传递给 facet_wrap 。经过一个令人沮丧的小时,我发现这只适用于 facet_grid !!! ???
因此,另一种方法是更改​​因子级别名称,以便为我提供我想要的方面标签::

  gdata $同位素<-revalue(x = ggdata $同位素,
c(14CAA=14C氨基酸,14CGlu=14C葡萄糖,
14cGlu6P=14C葡萄糖磷酸盐),33P=33P磷酸盐))

这有效,但我现在遇到的问题是我想要标签中的数字是超级脚本的。任何人都可以提出实现这个目标的最佳方法吗
谢谢

解决方案

管理整理出来!
在安装 curl devtools之后安装开发版 ggplot 时遇到问题然后重新安装比例它就起作用了。我尝试了@ eipi10答案,但无法让它工作,所以我以不同的方式更改了因子标签名称:

  ggdata $ Isotope < - 因子(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 函数:

< pre $ plot_func < - 函数(T){site_plots <-ggplot(data = T)+ geom_point(aes(Time,mean,color = Season,shape = Season))+
geom_line(aes(Time,mean,color = Season,linetype = Season))+
geom_errorbar(aes(Time,mean,ymax =(mean + se),ymin =(mean -se) width = 2)+
labs(title = T $ Site [1],y =解决方案中剩余同位素的百分比,x =时间(h))+
scale_x_continuous(breaks = c (0,24,48,72))+
scale_y_continuous(限制= c(0,115),休息= 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 =传递 ggdata

c $ c>到 plot_func 给了我下面的图和正确的facet标签。


I've hit a stumbling block in writing a ggplot function. I'm trying to change the facet labels in a ggplot facet_wrap plot.... but its proving trickier than I though it would be....

The data I am using can be accessed here

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 ...

I have written the following function to create the ggplot which uses the Isotope factor levels to label the facets:

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

Resulting in this lovely graph:

Which is nice but I want to change the facet labels now... Having done some poking around google I thought I might be able to use the labeller function as an argument to pass to facet_wrap. After a frustrating hour I discovered that this only works with facet_grid!!!??? So, an alternative method was to change the Factor level names so give me the facet labels that I want::

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

This works, but the problem I have now is that I want the numbers in the labels to be super-scripted. Can anyone suggest the best way to achieve this? Thanks

解决方案

Manage to sort it out! Had trouble installing the development version of ggplot but after installing curl and devtools and reinstalling scalesit worked. I tried @eipi10 answer but couldn't get that to work so I changed the factor label names in a different way:

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

I then adjusted the ggplot function to pass labeller = label_parsed to the facet_wrap function:

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

Passing the ggdata to the plot_func gives me the below graphs with the correct facet labels.

这篇关于ggplot在facet_wrap中重命名facet标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆