2

在我的 Macbook Pro 上,2015 年初,运行 High Sierra(如果这很重要) - 似乎 ggsave 忽略了我的 .png 文件的图像宽度和高度规范。我在运行 Windows 的机器上没有这个问题。这是一个可重现的小例子,但同样,如果您运行的是 Windows,则不会出现此问题:

# Setup Workspace
library("xlsx")
library("ggplot2")

# Generate Data
plotData <- data.frame(
  x = c("a", "b", "c", "d"),
  y = c(5,8,3,9)
)

# Create Plot
p <- ggplot() +
  geom_bar(data = plotData, aes(x,y), stat = "identity")
ggsave("example.png", width = 4, height = 3, units = "in")

#Add Plot to Workbook
wb <- createWorkbook(type = "xlsx")
sheet <- createSheet(wb, sheetName = "Example")
addPicture(file.path(getwd(), "example.png"), sheet, scale = 1, startRow = 3,
           startColumn = 2)
saveWorkbook(wb, file.path(getwd(),"example.xlsx"))

如果这是问题,有没有办法用 ggsave 解决这个问题?如果没有,有人可以建议另一种方法来保存具有特定大小的绘图以添加到带有 xlsx 包的工作簿中,该包可以在 Mac 和 Windows 机器上运行?

提前感谢您的帮助!

4

0 回答 0