2017-09-01 24 views
0

我目前正試圖將R markdown文檔編織爲html(或pdf),其中某個chunck中生成了多個圖。我通過chunck選項中的fig.cap參數指定字幕的標題,其長度與圖的數量相同。然而,爲了這個工作,cunck選項message必須是TRUE。 問題在gucplot2對象由chucnk中的函數生成時開始,我想用viridis::scale_fill_viridis應用新的填充。這很好,但不可避免地會拋出一條消息/警告,說明ggplot2對象已經有填充,並且viridis將替換它(Scale for 'fill' is already present. Adding another scale for 'fill', which will replace the existing scale)。我不想讓這個到我的markdown html的輸出中。顯然,使用suppressMessages也會抑制html(和pdf!)標題。 所以我的問題是:有沒有一種方法來「取消」現有的尺度屬性,以避免產生此消息?總之,我唯一的其他選擇是深入到首先生成對象的代碼中。或者:當cunck選項messages=F有什麼方法可以讓knitr保留字幕?取消設置ggplot2中的現有scale_fill_discrete或抑制新規模的消息

此致

FM

的最小工作實例將是的R降價文檔中的下列代碼:

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

如果相關,我的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 
+1

請提供[工作最小示例](https://stackoverflow.com/help/mcve),謝謝! – jaySf

+0

@jaySf我不清楚我如何提供需要針織的完整R降價文檔的MWE,但我會試一試。 –

回答

1

查看一個ggplot對象,這裏是p

str(p)

有很多的子結構。看看p$scales。這是一個ScalesList。下面可以幫助你:

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

+0

這個作品非常好!一個ggplot2對象非常複雜,謝謝幫助我找到自己的方式。 –