简体   繁体   English

ggplot2中的panel.border在CairoPDF设备的图的底部和右侧绘制较粗的线

[英]panel.border in ggplot2 ploting thicker lines in bottom and right side of the plot in CairoPDF device

在此处输入图片说明

I am using Cairo setting as 我正在使用开罗设置为

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

and theme setting is like this: 主题设置是这样的:

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"))

I guess, this is a problem with my theme setting for ggplot. 我想,这是我对ggplot设置主题的问题。 please give advice to fix it. 请提供建议以修复它。 Thanks. 谢谢。

Using only theme_bw() I can reproduce the feature. 仅使用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()

It seems that the combination of Cairo and ggplot2, when we use a theme, creates some rendering problems. 当我们使用主题时,Cairo和ggplot2的组合似乎会产生一些渲染问题。

One workaround is to save in simple pdf. 一种解决方法是将其保存为简单的pdf。 I hope that others give a better solution using this reproducible example. 我希望其他人可以使用此可复制示例提供更好的解决方案。

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

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). 如果不是,则问题出在绘图设备(内存中)和导出器(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. 在将绘图打印到屏幕上之前windows(width=8.3, height=11.7, xpinch=72, ypinch=72)请确保使用以下格式获取像素之间的精确匹配: windows(width=8.3, height=11.7, xpinch=72, ypinch=72)
  • 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. 如果您可以直接打印到PNG文件而没有问题,则问题可能出在CairoPDF导出器的工作方式上。 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. 您可能会发现,简单地使用其他文件格式或使用其他程序(例如LibreOffice或ImageMagick)将PNG(或其他文件)手动转换为PDF会更容易。

Good luck! 祝好运!

I have the same problem with bottom and right panel borders becoming thicker when sending the above ggplot example code to svg, pdf, cairo_pdf etc. 将上述ggplot示例代码发送到svg,pdf,cairo_pdf等时,底部和右侧面板的边框变粗,我也遇到同样的问题。

Here's a workaround that I use in Inkscape which might give some insight to someone with better understanding of R and pdf/svg rendering: 这是我在Inkscape中使用的一种解决方法,它可以使某些对R和pdf / svg渲染有更好理解的人有所了解:

In Inkscape: 在Inkscape中:

  • select the panel border 选择面板边框

  • object > ungroup (do this twice) 对象>取消分组(执行两次)

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

So it seems the panel border is grouped with the panel background. 因此,似乎面板边框与面板背景分组在一起。 Surprisingly rather than the bottom and right becoming thinner, the top and left become thicker. 令人惊讶的是,底部和右侧没有变薄,而是顶部和左侧变粗了。 So actually it looks like OP question should be rephrased to "plotting thinner lines in top and left side of plot". 因此,实际上,OP问题应该改写为“在图的顶部和左侧绘制更细的线”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM