ggplot 导出的抗锯齿
Anti-aliasing for ggplot exports
是否可以为导出的 ggplot 图获得良好的抗锯齿效果?我已经尝试了 Cairo
包以及一些不同的设备,但它们似乎都有锯齿状的边缘。
library(ggplot2)
library(Cairo)
p <- ggplot(data.frame(x=1:5,y=1:5),aes(x=x,y=y))+
geom_text(aes(2.5,2.5),label="Brown Fox bla bla..",size=5)+
labs(x=NULL,y=NULL)+
theme_bw()+
theme(plot.background=element_blank(),
plot.margin = margin(c(0,0,0,0)),
axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
axis.line = element_blank(),
panel.grid = element_blank(),
panel.border = element_blank(),
panel.background = element_blank())
png("test-nocairo.png",height=2,width=6,units="cm",res=300)
print(p)
dev.off()
png("test-cairo.png",height=2,width=6,units="cm",res=300,type="cairo")
print(p)
dev.off()
tiff("test-cairo.tiff",height=2,width=6,units="cm",res=300,type="cairo")
print(p)
dev.off()
ggsave("test-ggsave.png",height=2,width=6,units="cm",dpi=300,type="cairo")
PNG 无开罗
PNG 开罗
TIFF 开罗
PNG ggsave 开罗
为了我的目的,重要的是图像是 PNG 或 TIFF(无损),分辨率为 300dpi。我知道我可以导出为矢量格式(SVG、PDF 等),然后使用另一个程序转换为 PNG/TIFF,但这显然是额外的工作。我很好奇 R 中是否有任何我忽略的解决方案。
供参考,以上是 photoshop 渲染的质量。 PNG Arial 14pt.
好的。我可能在这里偶然发现了一些东西。当我使用 annotate
而不是 geom_text
时,cairo
抗锯齿似乎起作用了。
p <- ggplot(data.frame(x=1:5,y=1:5),aes(x=x,y=y))+
annotate("text",x=2.5,y=2.5,label="Brown Fox bla bla..",size=5)+
labs(x=NULL,y=NULL)+
theme_bw()+
theme(plot.background=element_blank(),
plot.margin = margin(c(0,0,0,0)),
axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
axis.line = element_blank(),
panel.grid = element_blank(),
panel.border = element_blank(),
panel.background = element_blank())
png("test-annotate-cairo.png",height=2,width=6,units="cm",res=300,type="cairo")
print(p)
dev.off()
所以,似乎 geom_text
过度绘制了可能是问题所在的相同文本。我认为这个过度绘制的东西在某个时候是固定的。我认为抗锯齿还有改进的空间,但是比以前好多了。
是否可以为导出的 ggplot 图获得良好的抗锯齿效果?我已经尝试了 Cairo
包以及一些不同的设备,但它们似乎都有锯齿状的边缘。
library(ggplot2)
library(Cairo)
p <- ggplot(data.frame(x=1:5,y=1:5),aes(x=x,y=y))+
geom_text(aes(2.5,2.5),label="Brown Fox bla bla..",size=5)+
labs(x=NULL,y=NULL)+
theme_bw()+
theme(plot.background=element_blank(),
plot.margin = margin(c(0,0,0,0)),
axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
axis.line = element_blank(),
panel.grid = element_blank(),
panel.border = element_blank(),
panel.background = element_blank())
png("test-nocairo.png",height=2,width=6,units="cm",res=300)
print(p)
dev.off()
png("test-cairo.png",height=2,width=6,units="cm",res=300,type="cairo")
print(p)
dev.off()
tiff("test-cairo.tiff",height=2,width=6,units="cm",res=300,type="cairo")
print(p)
dev.off()
ggsave("test-ggsave.png",height=2,width=6,units="cm",dpi=300,type="cairo")
为了我的目的,重要的是图像是 PNG 或 TIFF(无损),分辨率为 300dpi。我知道我可以导出为矢量格式(SVG、PDF 等),然后使用另一个程序转换为 PNG/TIFF,但这显然是额外的工作。我很好奇 R 中是否有任何我忽略的解决方案。
供参考,以上是 photoshop 渲染的质量。 PNG Arial 14pt.
好的。我可能在这里偶然发现了一些东西。当我使用 annotate
而不是 geom_text
时,cairo
抗锯齿似乎起作用了。
p <- ggplot(data.frame(x=1:5,y=1:5),aes(x=x,y=y))+
annotate("text",x=2.5,y=2.5,label="Brown Fox bla bla..",size=5)+
labs(x=NULL,y=NULL)+
theme_bw()+
theme(plot.background=element_blank(),
plot.margin = margin(c(0,0,0,0)),
axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
axis.line = element_blank(),
panel.grid = element_blank(),
panel.border = element_blank(),
panel.background = element_blank())
png("test-annotate-cairo.png",height=2,width=6,units="cm",res=300,type="cairo")
print(p)
dev.off()
所以,似乎 geom_text
过度绘制了可能是问题所在的相同文本。我认为这个过度绘制的东西在某个时候是固定的。我认为抗锯齿还有改进的空间,但是比以前好多了。