如何在工作表中插入箱线图

how to insert a boxplot in a worksheet

我想在 R 中创建的工作表中包含箱线图

here 我在“将绘图添加到 Excel 工作表”下找到了一个有用的方法 它使用 addPicture 函数 但是 sheet <-createSheet(wb, sheetName = "boxplot") 在 lybrary xlsx 中不起作用,XLConnect 也不会安装。

我试着修改成这样:

addWorksheet(wb,  "boxplot")
    sheets <- getSheets(wb)

现在我得到这个错误

> Error in envRefInferField(x, what, getClass(class(x)), selfEnv) :   
> ‘getNumberOfSheets’ is not a valid field or method name for reference
> class “Workbook” 
> 5. stop(gettextf("%s is not a valid field or method name for reference class %s", 
>     sQuote(field), dQuote(thisClass@className)), domain = NA) 
> 4. envRefInferField(x, what, getClass(class(x)), selfEnv) 
> 3. wb$getNumberOfSheets 
> 2. wb$getNumberOfSheets 
> 1. getSheets(doc)

我是不是弄乱了我的库,getSheets 调用了一个带有无效引用的函数? 或者有没有其他方法可以在没有 XLConnect pakage

的情况下将箱线图保存在工作表中

因为 xlsx 似乎不适合你,你可以尝试非常有用的包 openxlsx (no Java dependency,非常快,非常通用,尽管它只支持处理“.xlsx”文件):

install.packages("openxlsx")

wb <- openxlsx::createWorkbook()
openxlsx::addWorksheet(wb, "a_sheet_with_a_plot")
plot(cars)
openxlsx::insertPlot(wb, sheet = "a_sheet_with_a_plot", width = 10, height = 8, xy = NULL, startRow = 1,
                     startCol = 1, fileType = "png", units = "in", dpi = 300)
openxlsx::saveWorkbook(wb, file = "excel_file_with_a_plot_sheet.xlsx", overwrite = TRUE)