【发布时间】:2015-02-14 06:17:36
【问题描述】:
我将数据绘制成两个这样的图:
xy <- structure(list(NAME = structure(c(2L, 2L, 1L, 1L), .Label = c("CISCO", "JOHN"), class = "factor"), ID = c(41L, 41L, 57L, 57L), X_START_YEAR = c(1965L, 1932L, 1998L, 1956L), Y_START_VALUE = c(960L, -45L, 22L, -570L), X_END_YEAR = c(1968L, 1955L, 2002L, 1970L), Y_END_VALUE = c(960L, -45L, 22L, -570L), LC = structure(c(1L, 1L, 2L, 2L), .Label = c("CA", "US"), class = "factor")), .Names = c("NAME", "ID", "X_START_YEAR","Y_START_VALUE", "X_END_YEAR", "Y_END_VALUE", "LC"), class = "data.frame", row.names = c(NA,-4L))
ind <- split(xy,xy$ID)
# Plots
for (i in ind){
xx = unlist(i[,grep('X_',colnames(i))])
yy = unlist(i[,grep('Y_',colnames(i))])
fname <- paste0(i[1, 'ID'],'.png')
png(fname, width=1679, height=1165, res=150)
par(mar=c(6,8,6,5))
plot(xx,yy,type='n', xlab=NA, ylab="Value [mm]",ylim = range(c(yy,-.5,.5)))
i <- i[,-1]
segments(i[,2],i[,3],i[,4],i[,5],lwd=2)
abline(h=0)
dev.off()
}
之后,我将生成的 png 添加到一个这样的 png 文件中。
library(grid)
library(png)
plots <- lapply(ll <- list.files(patt='.*[.]png'),function(x){
img <- as.raster(readPNG(x))
rasterGrob(img, interpolate = FALSE)
})
library(ggplot2)
library(gridExtra)
ggsave("Plots_Combined.png",width=8.5, height=11,
do.call(marrangeGrob, c(plots, list(nrow=2, ncol=1,top=NULL))))
我的问题:我想知道在 R 中创建单个 png 后是否可以裁剪它们的侧面?我知道您可以通过更改 mar 参数在绘图循环中更改此设置,但我想知道您是否也可以在创建 png 后在外部执行此操作?我主要对裁剪图 1 的下部(41.png)和图 2 的上部(57.png)-> 应该裁剪下面示例草图中的红色区域感兴趣。是否可以在生成的 png 中添加标题或简单文本(例如标题)?
【问题讨论】: