R中保存pdf文档的两种方法:
- pdf+dev.off()
pdf(file = if(onefile) "Rplots.pdf" else "Rplot%03d.pdf",
width, height, onefile, family, title, fonts, version,
paper, encoding, bg, fg, pointsize, pagecentre, colormodel,
useDingbats, useKerning, fillOddEven, compress)
其中,参数width和height的默认单位为英寸,要以cm为单位需要进行转换
# Create a 8 cm* 8 cm file
pdf(<outfile>,width=8/2.54,height=8/2.54)
print(ggplot(...)) #支持print函数
dev.off()
- ggsave()
ggsave(filename, plot = last_plot(), device = NULL, path = NULL,
scale = 1, width = NA, height = NA, units = c("in", "cm", "mm"),
dpi = 300, limitsize = TRUE, ...)
#指定单位
ggsave(filename, width=8, height = 8, unit='cm')
#不需要手动关闭图形设备
输出png文件
#png指定的width和height单位为像素
png(filename,width=400,height=400)
...
dev.off()
#或ggsave
ggsave(filename,width=8,height=8,unit='cm',dpi=300)
关于dpi的说明:
分辨率,实际上控制的是 像素/英寸 (pixels per inch, ppi)
Linux下pdf转png
convert -units PixelsPerInch -density 300 <pdf file> <png file>
参数说明: convert parameters
-density geometry horizontal and vertical density of the image
-units type the units of image resolution
-monochrome transform image to black and white (黑白色)
-negate replace each pixel with its complementary color (反色)
-resize geometry resize the image,eg. 50% or 1024 X 1024 (单位为像素,中间字符为X)
-pointsize value font point size
参考网站:
字体相关
install.packages('extrafont')
library('extrafont')
#查找并保存系统中已安装的字体
font_import()
#列出字体
fonts()
#ggplot中关于字体的设置
text= element_text(size = 16,family = "Impact")