GuangchuangYu / hexSticker

:sparkles: Hexagon sticker in R
757 stars 167 forks source link

Strip above plot #121

Open MyKo101 opened 3 years ago

MyKo101 commented 3 years ago

The white_around_sticker=TRUE argument doesn't cover enough area. IT leaves a small strip at the top of the plot, if there is an overflow. See the below reprex for an example:

library(hexSticker)
library(ggplot2)

#Create a plain red plot that fills the plot window
p <- ggplot() + 
  theme_void() +
  theme(plot.background=element_rect(fill="red"))

#Create the sticker
s <- sticker(p,package = "",
             s_x=1,s_y=1,s_width=2,s_height=2.5,
             white_around_sticker=TRUE)
#Plot to see it
plot(s)
gangstR commented 3 years ago

I also have encountered this issue and was going to log it today. Thanks to @MyKo101 for beating me to it and also providing a clear reprex. For most of my color choices, I had not noticed the faint gray line there until now. Fortunately, I ordinarily use a full pkgdown build script that uses magick for my top-secret corporate packages (!?!?) since I need to do some post-processing of the sticker. That gave me an idea. So, in the interim, you can use magick (no pun intended) to work around this issue. I made a few small changes to the great reprex:

library(hexSticker)
library(ggplot2)
library(magick)

#Create a plain red plot that fills the plot window
p <- ggplot() +
  theme_void() +
  theme(plot.background=element_rect(fill="red"))

#Create the sticker.... and save it, too!
s <- sticker(p,package = "",
             s_x=1,s_y=1,s_width=2,s_height=2.5,
             white_around_sticker=TRUE, filename = "test.png")

#Plot to see it
plot(s)

# use some magick
s.img <- image_read("test.png")
# get the image details
s.info <- image_info(s.img)
# extract the bitmap
s.bitmap <- s.img[[1]]
# replace the top line of pixels with a white row
s.bitmap[, 1:s.info$width, 1] <- as.raw(c(0xff, 0xff, 0xff))
# convert the bitmap back and save
image_write(image_read(s.bitmap), "test.png", format = "png")

# Abracadabra!
plot(image_read("test.png"))
file.remove("test.png")   # a good reprex should clean up after itself :-)