Discard existing scale_fill_discrete in ggplot2 or suppress message for new scale

I am currently trying to link an R-marking document with html (or pdf), where in a specific chunck I create several charts. I specify the titles through the argument fig.capin the chunck parameters with a vector of the same length as the number of graphs. However, for this to work, the cunck parameter messagemust be TRUE. The problem starts when the ggplot2 object is generated by a function in chucnk, and I want to apply a new fill with viridis::scale_fill_viridis. Which is good, but inevitably throws a message / warning that there is already a fill applied to the ggplot2 object, and that 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 include this in the output of my httml markdown. Usage suppressMessagesalso seems to suppress html (and pdf!) Titles. So my question is:is there a way to “disable” the existing scaling attribute to avoid generating this message ? In short, 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 saves signatures when the cunck option messages=F?

Yours faithfully,

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 necessary, my output is session_info ():

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 
0
source share
1 answer

Take a look at the ggplot object, here p:

str(p)

There are many substructures. Take a look at p$scales. This is a ScalesList. The following may help you:

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

+1

Source: https://habr.com/ru/post/1664214/


All Articles