通过R中的xlsx包插入图表以实现excel

时间:2018-07-25 07:01:03

标签: r excel

对于一个我一直在从事的项目,我利用xlsx中的软件包R在Excel工作表中创建和设计报告。到目前为止,任何时候我想向工作表中插入分散点plot / histogram / density plot时,我都会创建一个ggplot2对象,将其保存为图像并将其插入到工作表中作为图像。

我的问题是,有没有办法使用xlsx包(或任何其他R包)向表格添加excel图表。例如,如果我有两列,一列的散点图与第二列的散点图,我希望该散点图为Excel图表,以便手动更改列中的数据将导致散点图的变化。有什么建议么?

1 个答案:

答案 0 :(得分:0)

遇到类似问题时,我发现此代码非常有用。它在一个工作表中创建报表粘贴数据框,在另一个工作表中创建图表。可悲的是,它确实将图表粘贴为图片。第二部分。

第二部分,我们在Excel中编辑数据,而不更改任何样式选项或其他任何内容。现在,您只需要手动创建Excel图表即可。这不是完美的灵魂,但它使我的生活更加轻松。希望对您有所帮助。

library(xlsx)

df <- iris

# create a new workbook for outputs
#++++++++++++++++++++++++++++++++++++
# possible values for type are : "xls" and "xlsx"
wb<-createWorkbook(type="xlsx")
# Define some cell styles
#++++++++++++++++++++++++++++++++++++
# Title and sub title styles
TITLE_STYLE <- CellStyle(wb)+ Font(wb,  heightInPoints=16, 
                                   color="blue", isBold=TRUE, underline=1)
SUB_TITLE_STYLE <- CellStyle(wb) + 
  Font(wb,  heightInPoints=14,
       isItalic=TRUE, isBold=FALSE)
# Styles for the data table row/column names
TABLE_ROWNAMES_STYLE <- CellStyle(wb) + Font(wb, isBold=TRUE)
TABLE_COLNAMES_STYLE <- CellStyle(wb) + Font(wb, isBold=TRUE) +
  Alignment(wrapText=TRUE, horizontal="ALIGN_CENTER") +
  Border(color="black", position=c("TOP", "BOTTOM"), 
         pen=c("BORDER_THIN", "BORDER_THICK")) 
# Create a new sheet in the workbook
#++++++++++++++++++++++++++++++++++++
sheet <- createSheet(wb, sheetName="US State Facts")
#++++++++++++++++++++++++
# Helper function to add titles
#++++++++++++++++++++++++
# - sheet : sheet object to contain the title
# - rowIndex : numeric value indicating the row to 
#contain the title
# - title : the text to use as title
# - titleStyle : style object to use for title
xlsx.addTitle<-function(sheet, rowIndex, title, titleStyle){
  rows <-createRow(sheet,rowIndex=rowIndex)
  sheetTitle <-createCell(rows, colIndex=1)
  setCellValue(sheetTitle[[1,1]], title)
  setCellStyle(sheetTitle[[1,1]], titleStyle)
}
# Add title and sub title into a worksheet
#++++++++++++++++++++++++++++++++++++
# Add title
xlsx.addTitle(sheet, rowIndex=1, title="US State Facts",
              titleStyle = TITLE_STYLE)
# Add sub title
xlsx.addTitle(sheet, rowIndex=2, 
              title="Data sets related to the 50 states of USA.",
              titleStyle = SUB_TITLE_STYLE)
# Add a table into a worksheet
#++++++++++++++++++++++++++++++++++++
addDataFrame(df, sheet , startRow=3, startColumn=1, 
             colnamesStyle = TABLE_COLNAMES_STYLE,
             rownamesStyle = TABLE_ROWNAMES_STYLE)
# Change column width
setColumnWidth(sheet, colIndex=c(1:ncol(state.x77)), colWidth=11)
# Add a plot into a worksheet
#++++++++++++++++++++++++++++++++++++
# create a png plot
png("boxplot.png", height=800, width=800, res=250, pointsize=8)
boxplot(count ~ spray, data = InsectSprays,
        col = "blue")
dev.off()
# Create a new sheet to contain the plot
sheet <-createSheet(wb, sheetName = "boxplot")
# Add title
xlsx.addTitle(sheet, rowIndex=1, title="Box plot using InsectSprays data",
              titleStyle = TITLE_STYLE)
# Add the plot created previously
addPicture("boxplot.png", sheet, scale = 1, startRow = 4,
           startColumn = 1)
# remove the plot from the disk
res<-file.remove("boxplot.png")
# Save the workbook to a file...
#++++++++++++++++++++++++++++++++++++
saveWorkbook(wb, "r-xlsx-report-example.xlsx")



# Here you have to restart R; XLConnect overlaps with xlsx --------------------------------------------------

require(XLConnect)

wb <- loadWorkbook("r-xlsx-report-example.xlsx")
imena <- getSheets(wb) #sheetsname
df <-  readWorksheet(wb, 
                     sheet = 1
                     ,startRow = 3)
df$Sepal.Length[df$Sepal.Length < 6] <- "my new data"

writeWorksheetToFile("r-xlsx-report-example.xlsx",df, sheet = imena[1],styleAction = XLC$STYLE_ACTION.NONE,header=T,startRow=3)
#saveWorkbook(wb,"r-xlsx-report-example.xlsx")