southwind
southwind

Reputation: 636

Blurry logo ggplot chart

I am trying to add my company logo under all of our charts. I managed to do so however the end result looks a bit disappointing. The logo looks all blurry despite me using high res image.

Is there any way I can improve this? (Note that the R logo I use has the same resolution as my company logo's res. They are all 1000*1000px)

Below is my code:

library(ggplot2)
library(png)
library(gridExtra)
library(grid)

dev.off(dev.list()["RStudioGD"])
gg <- ggplot(mtcars, aes(x = mpg, y = wt)) + 
  theme_minimal() +
  geom_count() + 
  labs(title = "Title Goes Here", x = "", y = "")

img <- readPNG("R-logo.png")
gg = gg + 
  annotation_custom(rasterGrob(img), 
                    xmin=0.95*min(mtcars$mpg)-1, xmax=0.95*min(mtcars$mpg)+1, 
                    ymin=0.6*min(mtcars$wt)-0.7, ymax=0.6*min(mtcars$wt)+0.5) +
  theme(plot.margin=margin(5,10,40,5))

# Turn off clipping
gt <- ggplot_gtable(ggplot_build(gg))
gt$layout$clip[gt$layout$name=="panel"] <- "off"
png('chart.png', width = 600, height = 500, units = "px",type='cairo',res=72)
grid.draw(gt)
dev.off()

enter image description here

Upvotes: 1

Views: 502

Answers (1)

southwind
southwind

Reputation: 636

enter image description here Actually, I found the way to properly scale the logo before laying over the plot. The magick package works wonderfully.

library(magick)
img <- image_read("R-logo.png")
img <- image_scale(img, "50")

Upvotes: 1

Related Questions