0

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.

Is it possible to let cairo_pdf decide the size of the PDF for readers to see the entire plot in the produced 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.

A code example:

cairo_pdf: Well-behaving PDF. The plot doesn't fit the 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("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()
Community
  • 1
  • 1
Anton Tarasenko
  • 8,099
  • 11
  • 66
  • 91

1 Answers1

1

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.

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()
IRTFM
  • 258,963
  • 21
  • 364
  • 487