当前位置:  开发笔记 > 前端 > 正文

ggplot和png的rmarkdown错误

如何解决《ggplot和png的rmarkdown错误》经验,为你挑选了1个好方法。

我已经尝试(徒劳)在Rmarkdown中生成带有ggplot的图表.代码如下:

```{r,echo=FALSE}
#fig.width=12,fig.height=6
panel2$PlotSize<-round(log(panel2$BSFA0200),0)- min(round(log(panel2$BSFA0200),0))+1# set size of dots
panel2$PlotSize[panel2$PlotSize==-Inf]<-NA
panel2$PlotColour<-ifelse(panel2$PlotSize<7,1,panel2$PlotSize)
panel2$PlotSize<-as.factor(panel2$PlotSize)
panel2$PlotColour<-as.factor(panel2$PlotColour)

g1<-ggplot(data=panel2,aes(x=NFR,y=PROF7*100,size=PlotSize,colour=PlotSize))+ geom_point()

g1

```

没有编织,这工作正常,但是当在Rmd文件(html或pdf)中执行时,我总是收到此错误消息

processing file: 1Profti_model.Rmd
  |..                                                               |   4%
  ordinary text without R code

  |.....                                                            |   8%
label: setup (with options) 
List of 1
 $ include: logi FALSE

  |........                                                         |  12%
  ordinary text without R code

  |..........                                                       |  15%
label: unnamed-chunk-1 (with options) 
List of 3
 $ echo   : logi FALSE
 $ warning: logi FALSE
 $ message: logi FALSE


Attaching package: 'dplyr'

The following objects are masked from 'package:stats':

    filter, lag

The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union

Loading required package: zoo

Attaching package: 'zoo'

The following objects are masked from 'package:base':

    as.Date, as.Date.numeric

  |............                                                     |  19%
   inline R code fragments

  |...............                                                  |  23%
label: unnamed-chunk-2 (with options) 
List of 1
 $ echo: logi FALSE

  |..................                                               |  27%
  ordinary text without R code

  |....................                                             |  31%
label: unnamed-chunk-3 (with options) 
List of 1
 $ echo: logi FALSE

Quitting from lines 98-109 (1Profti_model.Rmd) 
Error in png(..., res = dpi, units = "in") : unable to start png() device
Calls:  ... in_dir -> plot2dev -> do.call ->  -> png
In addition: Warning messages:
1: Removed 55 rows containing missing values (geom_point). 
2: In png(..., res = dpi, units = "in") :
  unable to open file '1Profti_model_files/figure-html/unnamed-chunk-3-1.png' for writing
3: In png(..., res = dpi, units = "in") : opening device failed

Execution halted

我还尝试通过将图表保存在png中然后将其加载为图片来解决此问题.也没有结果(请参阅在Rmd文件中加载png时出错)

谢谢你的帮助

更新:

根据你们中的一些人的建议,我添加了一个不同的块名称,我从Davit复制了我的数据代码(参见更新的代码).不幸的是,错误仍然存​​在.有趣的是,knitr不能写一个png,但可以在代码所在的同一个文件夹中写一个csv(我测试过它).

最后,我测试了将这个相同的代码运行到我的C驱动器上(惊喜!)它的工作原理.但是,这对我来说不是很有效,因为我不想依赖于特定的机器,我需要与其他人分享这项工作(因此必须使用网络驱动器).此外,所有其他包/代码在网络驱动器中工作正常,只有这个png()似乎是一个问题.

在此先感谢您的帮助!---标题:新文档作者:我输出:html_document ---

```{r prova,echo=FALSE, results='asis', message = FALSE, error = FALSE, warning= FALSE}
#.libPaths("D:/xxxx/packages")
require(ggplot2)

 panel2 <- data.frame(BSFA0200 = rnorm(100),
                        NFR = rnorm(100),
                        PROF7 = rnorm(100))

panel2$PlotSize<-round(log(panel2$BSFA0200),0)- min(round(log(panel2$BSFA0200),0))+1# set size of dots
panel2$PlotSize[panel2$PlotSize==-Inf]<-NA
panel2$PlotColour<-ifelse(panel2$PlotSize<7,2,panel2$PlotSize)

write.csv(panel2[1:100,c('BSFA0200',"NFR","PROF7")],file="test.csv")

g1 <- ggplot(data = panel2,
             aes(x = NFR,
                 y = PROF7 * 100,
                 size = factor(PlotSize),
                 colour = factor(PlotSize)
                 ))

g1 + geom_point()

```

错误输出:

Loading required package: ggplot2

Quitting from lines 9-32 (test.Rmd) 
Error in png(..., res = dpi, units = "in") : unable to start png() device
Calls:  ... in_dir -> plot2dev -> do.call ->  -> png
In addition: Warning messages:
1: Removed 35 rows containing missing values (geom_point). 
2: In png(..., res = dpi, units = "in") :
  unable to open file 'test_files/figure-html/prova-1.png' for writing
3: In png(..., res = dpi, units = "in") : opening device failed
Execution halted

我的knitr版本是1.11(它应该是最新版本),R版本是3.2.2

> R.Version()
$platform
[1] "i386-w64-mingw32"

$arch
[1] "i386"

$os
[1] "mingw32"

$system
[1] "i386, mingw32"

$status
[1] ""

$major
[1] "3"

$minor
[1] "2.2"

$year
[1] "2015"

$month
[1] "08"

$day
[1] "14"

$`svn rev`
[1] "69053"

$language
[1] "R"

$version.string
[1] "R version 3.2.2 (2015-08-14)"

$nickname
[1] "Fire Safety"

小智.. 5

我也收到了此消息。我的问题是文件路径太长。我的R markdown文件在太多子文件夹中,并且我的R markdown文件的名称太长。一旦我减少了文件路径的长度,问题就解决了。希望对您有用。



1> 小智..:

我也收到了此消息。我的问题是文件路径太长。我的R markdown文件在太多子文件夹中,并且我的R markdown文件的名称太长。一旦我减少了文件路径的长度,问题就解决了。希望对您有用。

推荐阅读
云聪京初瑞子_617
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有