R语言可视化存储矢量图实现方式

这篇文章主要为大家介绍了R语言存储矢量图的实现方式过程,有需要的朋友可以借鉴参考下,希望能够有所你帮助,祝大家多多进步,早日升职加薪

之前写的博客中有提及过如何在 R 语言中绘制矢量图,然后用于论文引用。但没有专门开一篇博客来进行说明比较,这里重新开一篇博客来进行说明。

通常保存为矢量图可能大多数时候是为了论文中的引用,所以格式一般为 EPS, PDF 这两种格式,这里也主要针对这两种格式进行说明。

1. R 中自带的默认绘图

通常我们使用 plot(), lines(), points(), hist() 等一些 R 中自带的绘图工具,如果我们想要将图片储存为矢量图的 PDF 格式应该怎么做呢?

1) PDF 格式

 pdf("example1.pdf", width = 4.0, height = 3.0) plot(rnorm(100), main="Hey Some Data") # 自己的绘图函数 # ... # ... dev.off() 

非常简单,只需用到 pdf() 函数即可。

2) EPS 格式

 setEPS() postscript("example1.eps", width = 4.0, height = 3.0) plot(rnorm(100), main="Hey Some Data") # 自己的绘图函数 # ... # ... dev.off() 

eps 格式相对复杂,需用到 setEPS()postscript() 函数。

2. ggplot 绘图

利用 ggplot 绘制矢量图就相对更加简单了,每种方式都只需在最后加上一行代码即可。

假设我们先利用 ggplot 进行绘图(用例子中的图):

 library(ggplot2) # Generate some sample data, then compute mean and standard deviation # in each group df <- data.frame( gp = factor(rep(letters[1:3], each = 10)), y = rnorm(30) ) ds <- plyr::ddply(df, "gp", plyr::summarise, mean = mean(y), sd = sd(y)) # The summary data frame ds is used to plot larger red points on top # of the raw data. Note that we don't need to supply `data` or `mapping` # in each layer because the defaults from ggplot() are used. ggplot(df, aes(gp, y)) + geom_point() + geom_point(data = ds, aes(y = mean), colour = 'red', size = 3) 

1) PDF 格式

这时我们要储存为 PDF 格式的图,只需在上述绘图语句后面运行下述语句即可:

 ggsave("example2.pdf", width = 4.0, height = 3.0) 

2) EPS 格式

而 EPS 格式需要多一个参数: device = cairo_ps

 ggsave("example2.eps", width = 4.0, height = 3.0, device = cairo_ps) 

以上就是R语言存储矢量图实现方式的详细内容,更多关于R语言存储矢量图的资料请关注0133技术站其它相关文章!

以上就是R语言可视化存储矢量图实现方式的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » R语言