2017-09-02 168 views
3

导出的ggplot图有可能得到很好的抗锯齿功能吗?我试过Cairo包以及几个不同的设备,但他们似乎都有锯齿边缘。ggplot导出的抗锯齿

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-nocairo PNG没有开罗

png-cairo PNG开罗

tiff-cairo TIFF开罗

png-ggsave-cairo PNG ggsave开罗

对于我目的,重要的是图像在300dpi时是PNG或TIFF(无损)。我知道我可以导出为矢量格式(SVG,PDF等),然后使用另一个程序转换为PNG/TIFF,但这显然是额外的工作。我很好奇,如果在R中有任何解决方案我可以忽略。

photoshop

作为参考,以上是从Photoshop渲染的质量。 PNG Arial 14pt。

回答

1

好的。我可能在这里偶然发现了一些东西。当我使用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() 

png-annotate-cairo

所以,好像geom_text是overplotting这可能是问题的相同的文字。我认为这种重叠绘图的事情在某个时刻是固定的。我认为抗锯齿还有改进的空间,但这比以前好得多。

+0

是的,用户有误,与抗锯齿无关。 'geom_text'在你原来的代码中,就像它应该的那样。这就是为什么'注释'存在。或者,为文本图层使用另一个数据集。 – baptiste