Stata 绘制图表如何保存成 400dpi 的高清 png 图片

Stata 保存图表的时候并不支持 dpi 的设置,很多期刊又要求提供 dpi = 300、400 之类的图表,常常让大家非常头大。

在 Stata 中保存图表的时候可以使用 width() 参数提升图片的清晰度:

cd "~/Desktop/Stata 绘制图表如何保存成 400dpi 的高清 png 图片"
*- 使用下面四幅图的组合展示
sysuse auto, clear
* 线图 + 直方图
tw histogram mpg, width(5) ysc(alt axis(1)) || ///
line weight mpg, yaxis(2) ysc(alt axis(2)) sort ///
name(a, replace) scheme(lightrstata)

*- 线图 + 阴影图
sysuse auto, clear
sum price, mean
local mean = r(mean)
qui kdensity price, gen(x h) nodraw
tw line h x || ///
area h x if x < `mean', name(b, replace) ///
scheme(lightrstata)

*- 散点图 + 拟合
tw sc price weight || ///
lpolyci price weight, name(c, replace) ///
scheme(lightrstata)

*- 箱线图
generate order = _n
expand 3
bysort order : generate which = _n
drop if which == 1 & price > 5000
drop if which == 2 & price > 10000
label def which 1 "<= $5000" 2 "<= $10000" 3 "all"
label val which which
gr box mpg, over(which) over(foreign) ///
name(d, replace) scheme(lightrstata)

gr combine a b c d, rows(2) scheme(lightrstata) xsize(20) ysize(12)
gr export pic1.png, width(4800) replace

在 Mac 系统上可以使用下面的代码查看图片的属性(Windows 电脑可以对着图片右键在属性里面查看):

!sips -g all pic1.png

*> /Users/ac/Desktop/Stata 绘制图表如何保存成 400dpi 的高清 png 图片/pic1.png
*> pixelWidth: 4800
*> pixelHeight: 2880
*> typeIdentifier: public.png
*> format: png
*> formatOptions: default
*> dpiWidth: 72.000
*> dpiHeight: 72.000
*> samplesPerPixel: 3
*> bitsPerSample: 8
*> hasAlpha: no
*> space: RGB
*> profile: sRGB IEC61966-2.1

可以看到这个图片还是 dpiWidth = 72 的,这是因为 width() 的设置只是单纯增加了图片的画幅,并没有增加图片的分辨率。

因此只能通过其他方式来解决了。比较方便的一个方法就是先导出成 pdf 文件,然后再通过其他方法把 pdf 文件转换成指定 dpi 的 png 图片。

方法一:使用 ImageMagick (命令行工具)

安装:https://imagemagick.org/script/download.php 。根据自己的操作系统选择下载安装即可。

在 Stata 中保存 pdf 文件:

gr export pic1.pdf, replace

然后就可以用 magick 命令转换了,这实际上是个命令行工具,需要在 DOS(Windows)或者 Terminal(Mac)里面运行,在 Stata 中可以在前面加上 shell 或者 ! 来调用:

!magick -density 400 pic1.pdf -set units PixelsPerInch -resample 400 output_400dpi.png

!sips -g all output_400dpi.png

*> /Users/ac/Desktop/Stata 绘制图表如何保存成 400dpi 的高清 png 图片/output_400dpi.png
*> pixelWidth: 8000
*> pixelHeight: 4800
*> typeIdentifier: public.png
*> format: png
*> formatOptions: default
*> dpiWidth: 400.000
*> dpiHeight: 400.000
*> samplesPerPixel: 4
*> bitsPerSample: 8
*> hasAlpha: yes
*> space: RGB

这个时候的文件就是 dpi = 400 的了。

方法二:使用在线网站

为了方便大家使用,我借助豆包编写了一个网页应用,可以快速的进行这种 pdf 到 png 的转换,并且可以自行设置 dpi:https://tidyfriday.cn/books/

然后点击下载即可:

如果 pdf 文件包含多个页面,点击下载全部即可全部下载。

方法三:使用 R 语言

如果大家会一些 R 语言,也可以用 R 语言完成这种转换:

library(magick)

# 转换PDF为400 DPI的PNG
convert_pdf_to_png <- function(pdf_path, output_path, dpi = 400) {
# 读取PDF并以指定密度渲染
img <- image_read_pdf(pdf_path, density = dpi)

# 保存为PNG并设置DPI元数据
image_write(img,
path = output_path,
format = "png",
density = paste0(dpi, "x", dpi))

# 验证结果
info <- image_info(img)
cat(sprintf("输出文件: %s\n", output_path))
cat(sprintf("像素尺寸: %dx%d\n", info$width, info$height))
cat(sprintf("物理密度: %dx%d pixels per inch\n", dpi, dpi))
}

# 使用示例
convert_pdf_to_png("pic1.pdf", "output_400dpi_withR.png", dpi = 400)

方法四:CloudConvert 网站

CloudConvert 网站也能够实现这种转换:https://cloudconvert.com/pdf-to-png

不过这个网站是需要把文件上传到 CloudConvert 的服务器,转换后再从服务器下载。这样的方式效率会比我编写的那个低很多。

更多好用的 RStata 工具

获取更多好用的 RStata 工具,建议收藏 RStata 的博客:https://tidyfriday.cn/

点击这里跳转到 RStata 短书平台获取附件:Stata 绘制图表如何保存成 400dpi 的高清 png 图片

评论