0

在此处输入图像描述

我正在使用开罗设置作为

CairoPDF(file = "test2.pdf", width = 8.3, height = 11.7) 

主题设置是这样的:

mytheme<-theme_bw() +
         theme(plot.title = element_text(lineheight=.8, face="bold"),
               text=element_text(size=11, family="Times New Roman"))+ 
         theme(panel.border = element_rect(linetype = "dashed", 
                                           colour = "black"))+ 
         theme(plot.margin = unit(c(1,1.5,1,1.5), "inches"))

我想,这是我的 ggplot 主题设置的问题。请提供建议以解决它。谢谢。

4

3 回答 3

3

仅使用theme_bw()我可以重现该功能。

df <- data.frame(lab = c('D1','D2','D3'),y = c(4,8,10),x= c(1,2,3))
library(Cairo)
#CairoPDF(file = "test2.pdf", width = 8.3, height = 11.7) 
library(grid)
library(ggplot2)
ggplot(df, aes(xmin = x-0.2, xmax = x + 0.2, ymin = 0, ymax = y,fill=lab)) +
  geom_rect()+xlim(labels = as.character(df$lab))+ theme_bw() 
dev.off()

似乎 Cairo 和 ggplot2 的组合,当我们使用主题时,会产生一些渲染问题。

一种解决方法是保存为简单的 pdf。我希望其他人使用这个可重现的示例给出更好的解决方案。

pdf(file = "test2.pdf", width = 8.3, height = 11.7) 
于 2013-01-28T16:01:01.677 回答
1

This may or may not fix your specific problem, but I often have this issue with 'grid' objects with respect to the pixel scaling. I can frequently fix the problem by slightly reducing or increasing the size of the image.

I would troubleshoot by taking the following steps:

  • Print the object to a screen plotting device, and see if the problem is still there. If not, the problem exists between your plotting device (in memory) and the exporter (CairoPDF). In that case, print to the screen first, and then save the file. Make sure the screen plotting device has the appropriate size.
  • Try adjusting the size of the 'width' and 'height' arguments. If you see the problem changing or others popping up, then you have a problem with scaling between the size of the plotting device and the export file. Make sure you get a pixel-exact match between the two, by using the format of: windows(width=8.3, height=11.7, xpinch=72, ypinch=72) before you print the plot to screen.
  • Test exporting directly to a different format. It may be that the problem is only in the specific exporter. If you can print directly to a PNG file without a problem, then the issue is probably with the way the CairoPDF exporter is working. You may find it easier to simply use the other file format or to manually convert the PNG (or other file) into a PDF using a different program, like LibreOffice or ImageMagick.

Good luck!

于 2013-01-28T15:24:13.620 回答
0

将上述 ggplot 示例代码发送到 svg、pdf、cairo_pdf 等时,底部和右侧面板边框变粗时遇到同样的问题。

这是我在 Inkscape 中使用的一种解决方法,它可能会给对 R 和 pdf/svg 渲染有更好理解的人提供一些见解:

在 Inkscape 中:

  • 选择面板边框

  • 对象 > 取消组合(这样做两次)

  • 或键盘快捷键 shift+ctrl+G (x2)

因此,面板边框似乎与面板背景组合在一起。令人惊讶的是,不是底部和右侧变薄,而是顶部和左侧变厚。所以实际上看起来OP问题应该改写为“在情节的顶部和左侧绘制更细的线”。

于 2017-11-14T09:06:56.083 回答