Michele
Michele

Reputation: 8753

CairoPNG increases text size outside plot margins

I have a chart created with ggplot2 and I'd like to use CairoPNG because especially when creating pie chart png and jpeg create a very pixilated image. The problem is that CairoPNG seems to modify the text size and so, especially in the legend, the text of one key overlaps other key, or, like in the above

library(ggplot2)
library(Cairo)

df <- data.frame(id=c("IMPORT VALUES YTD", "EXPORT VALUE YTD"),
                 value=c(6,4))

chart <- ggplot(df) +
  geom_bar(aes(x=factor(1), y=value, fill=factor(id)),
           stat="identity", width = 1, color="white") +
  coord_polar(theta="y")  +
  theme(legend.title=element_blank(),
        legend.position="top",
        legend.text=element_text(size=14))

CairoPNG("test1.png", 350, 400)
chart
dev.off()

enter image description here

png("test2.png", 350, 400)
chart
dev.off()

enter image description here

Do you know how to avoid this?

Upvotes: 4

Views: 429

Answers (1)

Andre Silva
Andre Silva

Reputation: 4928

This is a workaround adapted from @rcs answer. Add to your code:

library(grid)

and inside theme block:

plot.margin = unit(c(0,2,0,0), "lines")

enter image description here

Upvotes: 1

Related Questions