1

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

southwind
  • 636
  • 4
  • 15
  • Have you tried these https://stackoverflow.com/questions/9917049/inserting-an-image-to-ggplot2 – Tung Mar 02 '19 at 19:06
  • https://cran.r-project.org/web/packages/ggimage/vignettes/ggimage.html – Tung Mar 02 '19 at 19:07
  • if you look at the outputs from the above solution, the chart with logo is much blurrier compared to the one without. – southwind Mar 03 '19 at 01:01
  • How is the above image blurry? If anything, I'd call it overly sharp / pixelated, like what I'd expect from shrinking a high res raster image without proper resampling. – Z.Lin Mar 03 '19 at 04:44
  • Can you propose any method to properly scale it so it looks smoother? – southwind Mar 03 '19 at 09:21

1 Answers1

1

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")
southwind
  • 636
  • 4
  • 15