R: exporting ggplot2 text gets super small

Viewed 261

I made a graph with ggplot2 and in RStudio it looks really good. I also can export it manually. Unfortunately, when exporting it with code the text gets super small. I really need it to be exported by code.

RStudio: enter image description here

Code: enter image description here

Cairo::Cairo(
  1200, #length
  900, #width
  file = paste("cambridge_hashrate_countries_lollipop", ".png", sep = ""),
  type = "png", #tiff
  bg = "white", #white or transparent depending on your requirement 
  dpi = 300,
  units = "px" #you can change to pixels etc 
)

plot

logo <- image_read("../pics/logo_twitter-account.jpg")

grid::grid.raster(logo, x = 0.07, y = 0.03, just = c('left', 'bottom'), width = unit(0.5, 'inches'))
dev.off()

I also tried ggsave() same awful result.

How do I know what setting to choose to export it as RStudio?

Playing with dpi, height and width works, but then the labels are not properly displayed:

enter image description here

1 Answers

I think you must use png() function.

Can you try something like that:

png(filename = 'path/t1.png', width = 930, height = 430)

ggplot(data=df)+
  geom_bar(mapping = aes('x','y'), stat = 'identity')

dev.off()
Related