热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

ggplot在facet_wrap中重命名facet标签-ggplotrenamingfacetlabelsinfacet_wrap

Ivehitastumblingblockinwritingaggplotfunction.Imtryingtochangethefacetlabelsina

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

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

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:

我编写了以下函数来创建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)}

Resulting in this lovely graph:

导致这个可爱的图表:

enter image description here

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

哪个好,但我想现在改变facet标签...做了一些pogle周围的谷歌我认为我可能能够使用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"))

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

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

2 个解决方案

#1


6  

Set the facet labels to the appropriate expressions, then use the labeller function label_parsed to ensure that they are displayed properly. Here's an example, using the built-in iris data frame:

将构面标签设置为适当的表达式,然后使用labeller函数label_parsed确保它们正确显示。这是一个例子,使用内置的虹膜数据帧:

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)

You need to add the NULL before ^14*C or you'll get an error due to having ^ as the initial character. * and ~ mark the boundaries of each part of the expression, depending on whether you don't or do want a space between each part.

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

As of this writing (Dec. 12, 2015), you need to use the development version of ggplot2 for this to work with facet_wrap. However, this feature will presumably soon be incorporated into a regular release of the package.

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

enter image description here

#2


3  

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:

管理以解决它!在安装ggp​​lot的开发版本但安装curl和devtools并重新安装scaleit之后遇到了麻烦。我试过@ 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"))

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

然后我调整了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)}

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

将ggdata传递给plot_func会给出下面带有正确facet标签的图表。

enter image description here


推荐阅读
  • Docker的安全基准
    nsitionalENhttp:www.w3.orgTRxhtml1DTDxhtml1-transitional.dtd ... [详细]
  • 本文详细介绍了Java中org.eclipse.ui.forms.widgets.ExpandableComposite类的addExpansionListener()方法,并提供了多个实际代码示例,帮助开发者更好地理解和使用该方法。这些示例来源于多个知名开源项目,具有很高的参考价值。 ... [详细]
  • 本文详细介绍了macOS系统的核心组件,包括如何管理其安全特性——系统完整性保护(SIP),并探讨了不同版本的更新亮点。对于使用macOS系统的用户来说,了解这些信息有助于更好地管理和优化系统性能。 ... [详细]
  • 深入解析 Apache Shiro 安全框架架构
    本文详细介绍了 Apache Shiro,一个强大且灵活的开源安全框架。Shiro 专注于简化身份验证、授权、会话管理和加密等复杂的安全操作,使开发者能够更轻松地保护应用程序。其核心目标是提供易于使用和理解的API,同时确保高度的安全性和灵活性。 ... [详细]
  • 探讨如何真正掌握Java EE,包括所需技能、工具和实践经验。资深软件教学总监李刚分享了对毕业生简历中常见问题的看法,并提供了详尽的标准。 ... [详细]
  • 本文详细介绍了Java中org.neo4j.helpers.collection.Iterators.single()方法的功能、使用场景及代码示例,帮助开发者更好地理解和应用该方法。 ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 技术分享:从动态网站提取站点密钥的解决方案
    本文探讨了如何从动态网站中提取站点密钥,特别是针对验证码(reCAPTCHA)的处理方法。通过结合Selenium和requests库,提供了详细的代码示例和优化建议。 ... [详细]
  • Java 中的 BigDecimal pow()方法,示例 ... [详细]
  • Google最新推出的嵌入AI技术的便携式相机Clips现已上架,旨在通过人工智能技术自动捕捉用户生活中值得纪念的时刻,帮助人们减少照片数量过多的问题。 ... [详细]
  • 本文探讨了如何在 PHP 的 Eloquent ORM 中实现数据表之间的关联查询,并通过具体示例详细解释了如何将关联数据嵌入到查询结果中。这不仅提高了数据查询的效率,还简化了代码逻辑。 ... [详细]
  • 深入解析 Spring Security 用户认证机制
    本文将详细介绍 Spring Security 中用户登录认证的核心流程,重点分析 AbstractAuthenticationProcessingFilter 和 AuthenticationManager 的工作原理。通过理解这些组件的实现,读者可以更好地掌握 Spring Security 的认证机制。 ... [详细]
  • 本文探讨了如何在日常工作中通过优化效率和深入研究核心技术,将技术和知识转化为实际收益。文章结合个人经验,分享了提高工作效率、掌握高价值技能以及选择合适工作环境的方法,帮助读者更好地实现技术变现。 ... [详细]
  • 本文详细介绍了 org.apache.commons.io.IOCase 类中的 checkCompareTo() 方法,通过多个代码示例展示其在不同场景下的使用方法。 ... [详细]
  • Kubernetes 持久化存储与数据卷详解
    本文深入探讨 Kubernetes 中持久化存储的使用场景、PV/PVC/StorageClass 的基本操作及其实现原理,旨在帮助读者理解如何高效管理容器化应用的数据持久化需求。 ... [详细]
author-avatar
浅笑那段情2502918773
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有