我在寫一個需要精確尺寸的網頁應用程序。我決定使用ggplot2製作數字,因爲它們需要R的專業文本。我希望創建的數字沒有邊距,因爲它們將通過JavaScript旋轉。我使用這個頁面來了解如何減少邊距:https://kohske.wordpress.com/2010/12/25/drawing-on-full-region-in-ggplot2/,但無法打印到無邊界的.png文件。這裏是示例代碼。保存沒有頁邊空白的ggplot2對象的png
library(ggplot2)
library(gtable)
circle <- function(center = c(0,0),diameter = 1, npoints = 100){
r = diameter/2
tt <- seq(0,2*pi,length.out = npoints)
xx <- center[1] + r * cos(tt)
yy <- center[2] + r * sin(tt)
data.frame(x = xx, y = yy)
}
dat <- circle(c(0,0),1,npoints = 1000)
plot1 <- ggplot(dat,aes(x,y)) +
geom_path() +
theme(axis.text.y=element_blank(),
axis.text.x=element_blank(),
axis.ticks=element_blank(),
axis.ticks.length = unit(0,"null"),
axis.ticks.margin = unit(0,"null"),
axis.title.x=element_blank(),
axis.title.y=element_blank(),
legend.position="none",
panel.background = element_blank(),
panel.grid = element_blank(),
title = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.margin = unit(0,"null"),
plot.margin = rep(unit(0,"null"),4),
axis.ticks.length = unit(0,"cm"),
axis.ticks.margin = unit(0,"cm"))
png("plot.png", width=434, height=434)
print(plot1)
dev.off()
這輸出一個具有體面大小的邊界的圓。讓我準確地說我想要的是一個434x434像素的png,其中有一個沒有邊框的圓(例如圓的直徑是434px)。我可以創建一個更大的文件然後裁剪下來,但是我會製作大約50張這些圖形。謝謝你的幫助!
您可以從ggplot佈局中僅選擇繪圖面板。 [見使用ggplot-in-r-how-do-i-remove-margins-surround-the-plot-area](http://stackoverflow.com/questions/31254533/when-using-ggplot-in -R-怎麼辦-I-去除切緣全周的積區/ 31256788#31256788) –