4

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?

Andre Silva
  • 4,782
  • 9
  • 52
  • 65
Michele
  • 8,563
  • 6
  • 45
  • 72
  • From your two outputs it's not obvious to me that the size of the text is different. The quality of the text rendition is clearly different though (and it might not be the same font exactly). – plannapus Sep 11 '13 at 12:06
  • 2
    same or not they do occupy more space... in fact they don't fit into the plot – Michele Sep 11 '13 at 12:14

1 Answers1

1

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

Community
  • 1
  • 1
Andre Silva
  • 4,782
  • 9
  • 52
  • 65
  • Note you **maybe** have either an extra "s" on import valuES or an missing "s" on export valuE. If the former, then, your previous code already would work with one less character in the legend-title. – Andre Silva Sep 11 '13 at 15:38
  • Hi! thanks a lot. Yes, it is a typo... sorry. However this does not help when the left key overlaps the right one. My workaround is paste()ing each level with `" "`. I was actually after some parameter to trigger in `CairoPNG` to avoid that. But it seems more tricky than that. – Michele Sep 12 '13 at 09:11