在ggplot2中取消设置现有的scale_fill_discrete,或者抑制新的比例 [英] Unset existing scale_fill_discrete in ggplot2 or suppress message for new scale

查看:1361
本文介绍了在ggplot2中取消设置现有的scale_fill_discrete,或者抑制新的比例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正试图将R markdown文档编译为html(或pdf),在某个chunck中我生成了多个图。我通过chunck选项中的 fig.cap 参数指定字幕,其长度与图的数量相同。然而,为了这个工作,cunck选项 message 必须是 TRUE
当chgnk中的函数生成ggplot2对象时,问题就开始了,我想用 viridis :: scale_fill_viridis 来应用新的填充。这是好的,但不可避免地会抛出一个消息/警告,已经有一个填充应用于ggplot2对象,并且viridis将替换它( Scale的'fill'已经存在。填充',这将取代现有的比例
)。我不想让这个到我的markdown html的输出中。使用 suppressMessages 显然也会抑制html(和pdf!)标题。
所以我的问题是:是否有一种方法可以取消现有的scale属性以避免生成此消息?总之,我唯一的其他选择是深入到首先生成对象的代码中。或者:当cunck选项 messages = F



亲切的问候时,knitr会保留字幕的方式



FM



一个最简单的工作示例是R markdown文档中的以下代码:

  --- 
标题:SOtest
作者:FM Kerckhof
日期:1 / 9/2017
输出:html_document
---
$ b $```{r setup,include = FALSE}
library(ggplot2)
library (ggthemes)
library(viridis)
knitr :: opts_chunk $ set(echo = TRUE)
####自定义函数####
creategg< - function( x,plot = FALSE)
{
datnam < - names(x)
p < - ggplot(data = x,aes(x = x [,datnam [1]],
y = x [,datnam [2]],
fill = factor(x [,datnam [ncol(x)]]))+
geom_bar(stat =identity)+
scale_fill_gdocs()
if(plot == TRUE){
print(p)
}
reslist< - list(inputdata = x,ggplotobj = p)
}
```

## Lorem ipsum

实际报告

```{iriscars,fig.cap = c plot with default fill,with viridis fill),echo = FALSE}
a < - creategg(iris,plot = TRUE)
a $ ggplotobj + scale_fill_viridis(discrete = TRUE)
```

如果相关,我的session_info()输出是:

 会话信息------------------------------ -------------------------------------------------- ----------- 
设置值
version R版本3.4.1(2017-06-30)
系统x86_64,linux-gnu
ui RStudio (1.0.153)
语言(EN)
collat​​e en_US.UTF-8
tz欧洲/布鲁塞尔
日期2017-09-01

包裹-------------------------------------------------- ---------------------------------------------
包*版本日期源
ade4 1.7-8 2017-08-09 CRAN(R 3.4.1)
ape 4.1 2017-02-14 CRAN(R 3.3.2)
断言* 0.2.0 2017-04-11 CRAN (R 3.3.3)
backports 1.1.0 2017-05-22 CRAN(R 3.4.0)
基准* 3.4.1 2017-07-08当地
bindr 0.1 2016-11 -13 CRAN(R 3.4.0)
bindrcpp 0.2 2017-06-17 CRAN(R 3.4.0)
Biobase * 2.36.2 2017-05-09 Bioconductor
BiocGenerics * 0.22。 0 2017-05-04 Bioconductor
biomformat 1.4.0 2017-05-04 Bioconductor
Biostrings 2.44.2 2017-07-24 Bioconductor
bitops 1.0-6 2013-08-17 CRAN( R 3.2.4)
caTools 1.17.1 2014-09-10 CRAN(R 3.2.4)
集群2.0.6 2017-03-16 CRAN(R 3.4.0)
codetools 0.2-15 2016-10-05 CRAN(R 3.3.1)
colorspace 1.3-2 2016-12-14 CRAN(R 3.3.2)
编译器3.4.1 2017-07-08 local
data.table 1.10.4 2017-02-01 CRAN(R 3.3.2)
数据集s * 3.4.1 2017-07-08本地
devtools * 1.13.3 2017-08-02 CRAN(R 3.4.1)
摘要0.6.12 2017-01-27 CRAN(R 3.3。 2)
dplyr * 0.7.2 2017-07-20 CRAN(R 3.4.1)
评估值0.10.1 2017-06-24 CRAN(R 3.4.0)
extrafont * 0.17 2014-12-08 CRAN(R 3.4.0)
extrafontdb 1.0 2012-06-11 CRAN(R 3.4.0)
foreach 1.4.3 2015-10-13 CRAN(R 3.2.4)
gdata 2.18.0 2017-06-06 CRAN(R 3.4.0)
ggplot2 * 2.2.1 2016-12-30 CRAN(R 3.3.2)
ggthemes * 3.4.0 2017-02-19 CRAN(R 3.3.2)
胶水1.1.1 2017-06-21 CRAN(R 3.4.0)
gplots * 3.0.1 2016-03-30 CRAN(R 3.2 .4)
图形* 3.4.1 2017-07-08本地
grDevices * 3.4.1 2017-07-08本地
网格3.4.1 2017-07-08本地
gridExtra 2.2.1 2016-02-29 CRAN(R 3.3.1)
gtable 0.2.0 2016-02-26 CRAN(R 3.2.4)
gtools * 3。 5.0 2015-05-29 CRAN(R 3.2.4)
htmltools 0.3.6 2017-04-28 CRAN(R 3.4.0)
igraph 1.1.2 2017-07-21 CRAN(R 3.4 .1)
IRanges 2.10.2 2017-06-01 Bioconductor
迭代器1.0.8 2015-10-13 CRAN(R 3.2.4)
jsonlite 1.5 2017-06-01 CRAN( R 3.4.0)
KernSmooth 2.23-15 2015-06-29 CRAN(R 3.4.0)
knitr 1.17 2017-08-10 CRAN(R 3.4.1)
标签0.3 2014 -08-23 CRAN(R 3.2.4)
格子* 0.20-35 2017-03-25 CRAN(R 3.3.3)
lazyeval 0.2.0 2016-06-12 CRAN(R 3.3。 1)
magrittr 1.5 2014-11-22 CRAN(R 3.2.4)
MASS 7.3-47 2017-04-21 CRAN(R 3.4.0)
矩阵1.2-11 2017- 08-16 CRAN(R 3.4.1)
备忘录1.1.0 2017-04-21 CRAN(R 3.4.0)
方法* 3.4.1 2017-07-08当地
mgcv 1.8-19 2017-08-29 CRAN(R 3.4.1)
multtest 2.32.0 2017-05-04 Bioconductor
munsell 0.4.3 2016 -02-13 CRAN(R 3.2.4)
nlme 3.1-131 2017-02-06 CRAN(R 3.3.2)
并行* 3.4.1 2017-07-08当地
置换* 0.9-4 2016-09-09 CRAN(R 3.3.1)
phyloseq 1.20.0 2017-05-04 Bioconductor
pkgconfig 2.0.1 2017-03-21 CRAN(R 3.4.0 )
plyr * 1.8.4 2016-06-08 CRAN(R 3.3.1)
R6 2.2.2 2017-06-17 CRAN(R 3.4.0)
Rcpp 0.12.12 2017-07-15 CRAN(R 3.4.1)
reshape2 * 1.4.2 2016-10-22 CRAN(R 3.3.2)
rhdf5 2.20.0 2017-05-04 Bioconductor
rlang 0.1.2 2017-08-09 CRAN(R 3.4.1)
rmarkdown 1.6 2017-06-15 CRAN(R 3.4.0)
rprojroot 1.2 2017-01-16 CRAN(R 3.3 .2)
Rttf2pt1 1.3.4 2016-05-19 CRAN(R 3.4.0)
S4Vectors 0.14.3 2017-06-06 Bioconductor
比例0.5.0 2017-08-24 CRAN(R 3.4.1)
样条3.4.1 2017-07-08当地
统计* 3.4.1 2017-07-08 loc al
stats4 3.4.1 2017-07-08 local
stringi 1.1.5 2017-04-07 CRAN(R 3.3.3)
stringr 1.2.0 2017-02-18 CRAN( R 3.3.2)
生存2.41-3 2017-04-04 CRAN(R 3.3.3)
tibble 1.3.4 2017-08-22 CRAN(R 3.4.1)
工具3.4.1 2017-07-08本地
utils * 3.4.1 2017-07-08本地
纯素* 2.4-4 2017-08-24 CRAN(R 3.4.1)
viridis * 0.4.0 2017-03-27 CRAN(R 3.4.1)
viridisLite * 0.2.0 2017-03-24 CRAN(R 3.3.3)
withr 2.0.0 2017-07-28 CRAN(R 3.4.1)
XVector 0.16.0 2017-05-04 Bioconductor
yaml 2.1.14 2016-11-12 CRAN(R 3.3.2)
zlibbioc 1.22.0 2017 -05-04 Bioconductor


解决方案

看看ggplot对象,这里 p



str(p)

有很多子结构。看看 p $ scales 。这是一个ScalesList。以下内容可以帮助您:
$ b

i < - 其中(sapply(p $ scale $ scales,function(x)'fill'%in %x $ aesthetics))
p $ scales $ scales [[i]]< - NULL


I am currently trying to knit an R markdown document to html (or pdf) where in a certain chunck I generate multiple plots. I specify the captions through the fig.cap argument in the chunck options with a vector of the same length as the number of plots. However, for this to work the cunck option message has to be TRUE. The issue starts when a ggplot2 object is generated by a function in the chucnk and I want to apply a new fill with viridis::scale_fill_viridis. Which is fine, but inevitably throws a message/warning that there is already a fill applied to the ggplot2 object and that the viridis will replace it (Scale for 'fill' is already present. Adding another scale for 'fill', which will replace the existing scale). I do not want to get this into the output of my markdown html. Using suppressMessages apparently also suppresses the html (and pdf!) captions. So my question is: is there a way to "unset" the existing scales attribute to avoid this message being generated? Short from that my only other option would be to dive into the code that generated the object in the first place. Or: is there a way that knitr preserves captions when the cunck option messages=F?

Kind regards,

FM

A minimal working example would be the following code in an R markdown document:

---
title: "SOtest"
author: "FM Kerckhof"
date: "1/9/2017"
output: html_document
---

```{r setup, include=FALSE}
library(ggplot2)
library(ggthemes)
library(viridis)
knitr::opts_chunk$set(echo = TRUE)
#### custom function ####
creategg <- function(x,plot=FALSE)
{
  datnam <- names(x)
  p <- ggplot(data=x,aes(x=x[,datnam[1]],
                         y=x[,datnam[2]],
                         fill=factor(x[,datnam[ncol(x)]]))) + 
              geom_bar(stat="identity") + 
              scale_fill_gdocs()
  if(plot==TRUE){
    print(p)
  }
  reslist <- list(inputdata=x,ggplotobj=p)
}
```

## Lorem ipsum

Actual report

```{r iriscars, fig.cap=c("plot with default fill","same plot with viridis fill"), echo=FALSE}
a <- creategg(iris,plot=TRUE)
a$ggplotobj + scale_fill_viridis(discrete=TRUE)
```

If relevant, my session_info() output is:

Session info -------------------------------------------------------------------------------------------
 setting  value                       
 version  R version 3.4.1 (2017-06-30)
 system   x86_64, linux-gnu           
 ui       RStudio (1.0.153)           
 language (EN)                        
 collate  en_US.UTF-8                 
 tz       Europe/Brussels             
 date     2017-09-01                  

Packages -----------------------------------------------------------------------------------------------
 package      * version date       source        
 ade4           1.7-8   2017-08-09 CRAN (R 3.4.1)
 ape            4.1     2017-02-14 CRAN (R 3.3.2)
 assertthat   * 0.2.0   2017-04-11 CRAN (R 3.3.3)
 backports      1.1.0   2017-05-22 CRAN (R 3.4.0)
 base         * 3.4.1   2017-07-08 local         
 bindr          0.1     2016-11-13 CRAN (R 3.4.0)
 bindrcpp       0.2     2017-06-17 CRAN (R 3.4.0)
 Biobase      * 2.36.2  2017-05-09 Bioconductor  
 BiocGenerics * 0.22.0  2017-05-04 Bioconductor  
 biomformat     1.4.0   2017-05-04 Bioconductor  
 Biostrings     2.44.2  2017-07-24 Bioconductor  
 bitops         1.0-6   2013-08-17 CRAN (R 3.2.4)
 caTools        1.17.1  2014-09-10 CRAN (R 3.2.4)
 cluster        2.0.6   2017-03-16 CRAN (R 3.4.0)
 codetools      0.2-15  2016-10-05 CRAN (R 3.3.1)
 colorspace     1.3-2   2016-12-14 CRAN (R 3.3.2)
 compiler       3.4.1   2017-07-08 local         
 data.table     1.10.4  2017-02-01 CRAN (R 3.3.2)
 datasets     * 3.4.1   2017-07-08 local         
 devtools     * 1.13.3  2017-08-02 CRAN (R 3.4.1)
 digest         0.6.12  2017-01-27 CRAN (R 3.3.2)
 dplyr        * 0.7.2   2017-07-20 CRAN (R 3.4.1)
 evaluate       0.10.1  2017-06-24 CRAN (R 3.4.0)
 extrafont    * 0.17    2014-12-08 CRAN (R 3.4.0)
 extrafontdb    1.0     2012-06-11 CRAN (R 3.4.0)
 foreach        1.4.3   2015-10-13 CRAN (R 3.2.4)
 gdata          2.18.0  2017-06-06 CRAN (R 3.4.0)
 ggplot2      * 2.2.1   2016-12-30 CRAN (R 3.3.2)
 ggthemes     * 3.4.0   2017-02-19 CRAN (R 3.3.2)
 glue           1.1.1   2017-06-21 CRAN (R 3.4.0)
 gplots       * 3.0.1   2016-03-30 CRAN (R 3.2.4)
 graphics     * 3.4.1   2017-07-08 local         
 grDevices    * 3.4.1   2017-07-08 local         
 grid           3.4.1   2017-07-08 local         
 gridExtra      2.2.1   2016-02-29 CRAN (R 3.3.1)
 gtable         0.2.0   2016-02-26 CRAN (R 3.2.4)
 gtools       * 3.5.0   2015-05-29 CRAN (R 3.2.4)
 htmltools      0.3.6   2017-04-28 CRAN (R 3.4.0)
 igraph         1.1.2   2017-07-21 CRAN (R 3.4.1)
 IRanges        2.10.2  2017-06-01 Bioconductor  
 iterators      1.0.8   2015-10-13 CRAN (R 3.2.4)
 jsonlite       1.5     2017-06-01 CRAN (R 3.4.0)
 KernSmooth     2.23-15 2015-06-29 CRAN (R 3.4.0)
 knitr          1.17    2017-08-10 CRAN (R 3.4.1)
 labeling       0.3     2014-08-23 CRAN (R 3.2.4)
 lattice      * 0.20-35 2017-03-25 CRAN (R 3.3.3)
 lazyeval       0.2.0   2016-06-12 CRAN (R 3.3.1)
 magrittr       1.5     2014-11-22 CRAN (R 3.2.4)
 MASS           7.3-47  2017-04-21 CRAN (R 3.4.0)
 Matrix         1.2-11  2017-08-16 CRAN (R 3.4.1)
 memoise        1.1.0   2017-04-21 CRAN (R 3.4.0)
 methods      * 3.4.1   2017-07-08 local         
 mgcv           1.8-19  2017-08-29 CRAN (R 3.4.1)
 multtest       2.32.0  2017-05-04 Bioconductor  
 munsell        0.4.3   2016-02-13 CRAN (R 3.2.4)
 nlme           3.1-131 2017-02-06 CRAN (R 3.3.2)
 parallel     * 3.4.1   2017-07-08 local         
 permute      * 0.9-4   2016-09-09 CRAN (R 3.3.1)
 phyloseq       1.20.0  2017-05-04 Bioconductor  
 pkgconfig      2.0.1   2017-03-21 CRAN (R 3.4.0)
 plyr         * 1.8.4   2016-06-08 CRAN (R 3.3.1)
 R6             2.2.2   2017-06-17 CRAN (R 3.4.0)
 Rcpp           0.12.12 2017-07-15 CRAN (R 3.4.1)
 reshape2     * 1.4.2   2016-10-22 CRAN (R 3.3.2)
 rhdf5          2.20.0  2017-05-04 Bioconductor  
 rlang          0.1.2   2017-08-09 CRAN (R 3.4.1)
 rmarkdown      1.6     2017-06-15 CRAN (R 3.4.0)
 rprojroot      1.2     2017-01-16 CRAN (R 3.3.2)
 Rttf2pt1       1.3.4   2016-05-19 CRAN (R 3.4.0)
 S4Vectors      0.14.3  2017-06-06 Bioconductor  
 scales         0.5.0   2017-08-24 CRAN (R 3.4.1)
 splines        3.4.1   2017-07-08 local         
 stats        * 3.4.1   2017-07-08 local         
 stats4         3.4.1   2017-07-08 local         
 stringi        1.1.5   2017-04-07 CRAN (R 3.3.3)
 stringr        1.2.0   2017-02-18 CRAN (R 3.3.2)
 survival       2.41-3  2017-04-04 CRAN (R 3.3.3)
 tibble         1.3.4   2017-08-22 CRAN (R 3.4.1)
 tools          3.4.1   2017-07-08 local         
 utils        * 3.4.1   2017-07-08 local         
 vegan        * 2.4-4   2017-08-24 CRAN (R 3.4.1)
 viridis      * 0.4.0   2017-03-27 CRAN (R 3.4.1)
 viridisLite  * 0.2.0   2017-03-24 CRAN (R 3.3.3)
 withr          2.0.0   2017-07-28 CRAN (R 3.4.1)
 XVector        0.16.0  2017-05-04 Bioconductor  
 yaml           2.1.14  2016-11-12 CRAN (R 3.3.2)
 zlibbioc       1.22.0  2017-05-04 Bioconductor 

解决方案

Take a look at a ggplot object, here p:

str(p)

There's a lot of sub-structure. Take a look at p$scales. It's a ScalesList. The following might help you:

i <- which(sapply(p$scales$scales, function(x) 'fill' %in% x$aesthetics)) p$scales$scales[[i]] <- NULL

这篇关于在ggplot2中取消设置现有的scale_fill_discrete,或者抑制新的比例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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