简体   繁体   English

R:cairo_pdf输出大小和图大小

[英]R: cairo_pdf output size and plot size

When long text is placed along y-axis in ggplot and cairo_pdf is called without width and height , it uses width = 7, height = 7 parameters, so that wide plots can't fit the output PDF. 当在ggplot沿y轴放置长文本并且在不使用widthheight情况下调用cairo_pdf ,它将使用width = 7, height = 7参数,因此宽图无法适合输出PDF。

Is it possible to let cairo_pdf decide the size of the PDF for readers to see the entire plot in the produced PDF? 是否可以让cairo_pdf决定PDF的大小,以便读者查看所生成的PDF中的整个图?

Here one can find solution via ggsave , but it doesn't fit this case because ggsave has problems with non-Latin characters in PDF output. 在这里可以通过ggsave找到解决方案,但是它不适合这种情况,因为ggsave在PDF输出中存在非拉丁字符的问题。

A code example: 一个代码示例:

cairo_pdf : Well-behaving PDF. cairo_pdf :格式良好的PDF。 The plot doesn't fit the PDF. 该图不适合PDF。

cairo_pdf("out_cairo.pdf")
df <- data.frame(x = c("Несмотря на то, что доктора лечили его, пускали кровь и давали пить лекарства, он всё-таки выздоровел.", "B"), y = c(15, 20))
ggplot(df, aes(x = x, y = y)) +
  stat_summary(fun.y = "mean", geom = "bar") +
  coord_flip()
dev.off()

pdf : Text is overlapping. pdf :文本重叠。

pdf("out_pdf.pdf")
df <- data.frame(x = c("Несмотря на то, что доктора лечили его, пускали кровь и давали пить лекарства, он всё-таки выздоровел.", "B"), y = c(15, 20))
ggplot(df, aes(x = x, y = y)) +
  stat_summary(fun.y = "mean", geom = "bar") +
  coord_flip()
dev.off()

This makes room for a pathologically wide margin and a plot. 这为病理学上的裕度和情节留出了空间。 If you wnated this to be programmatic you could write a mycairo function that accepted a dataframe and used strwidth to find the maximum length of the printed x value. 如果拥有此功能是编程的,则可以编写一个mycairo函数,该函数接受一个数据框并使用strwidth查找打印的x值的最大长度。

df <- data.frame(x = c("Несмотря на то, что доктора лечили его, пускали кровь и давали пить лекарства, он всё-таки выздоровел.", "B"), y = c(15, 20))
ylablen <- strwidth(df$x[1])
cairo_pdf("out_cairo.pdf", width= ylablen+5)

ggplot(df, aes(x = x, y = y)) +
  stat_summary(fun.y = "mean", geom = "bar") +
  coord_flip()
dev.off()

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

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