Closed sgibb closed 7 years ago
Alternative approach: use a spotlight. I played a little bit around and found the following solution:
library(hexSticker)
library(ggplot2)
library(lattice)
library(grid)
library(hexbin)
p <- ggplot(aes(x = mpg, y = wt), data = mtcars) + geom_point()
## Add highlight:
set.seed(123)
vals_x <- rnorm(500000, sd = 2, mean = 0)
vals_y <- rnorm(500000, sd = 2, mean = 0)
whiteTrans <- function(n) {
rgb(r = rep(1, n), g = rep(1, n), b = rep(1, n),
alpha = seq(0, 0.4, length.out = n))
}
hgl <- hexbinplot(vals_x ~ vals_y, colramp = whiteTrans, colorkey = FALSE,
bty = "n", scales = list(draw = FALSE), xlab = "", ylab = "",
border = NA, par.settings = list(axis.line = list(col = NA)))
## Combine the plots; have to adjust x, y, width and height to the mtcars data
p <- p + geom_subview(hgl, x = 21.5, y = 2.2,
width = 50, height = 50) +
theme_void() + theme_transparent()
print(p)
sticker(p, package="hexSticker", p_size=8, s_x=1, s_y=.75, s_width=1.3, s_height=1,
filename="ggplot2_highlight.png")
it involves a little try and error but looks not too bad I think:
Somehow makes sense to have hexagon-shaped highlights in a hexagonal image.
@GuangchuangYu , do you think it could be included into hexSticker
? See related https://github.com/Bioconductor/BiocStickers/issues/27
require(ggplot2)
require(hexSticker)
p <- ggplot(aes(x = mpg, y = wt), data = mtcars) + geom_point() +
theme_void() + theme_transparent()
sticker(p, package="hexSticker", p_size=8,
s_x=1, s_y=.75, s_width=1.3, s_height=1,
spotlight=T, filename="ggplot2_highlight.png")
By default, spotlight=FALSE
. If we set spotlight=TRUE
, it will be added and parameters, l_x
, l_y
, l_width
and l_height
, will work for the spotlight
.
see https://github.com/GuangchuangYu/hexSticker/commit/5a442e7e148db88cdb5a235fc32ba4b23686180a.
Awesome @GuangchuangYu ! Just one minor comment: you think it would be possible to put the highlight on the very top layer? In your example the highlight is below the points, but it should be on top of the data points.
done!
@jotsetung actually, you can arrange layers after the sticker was created.
pg <- sticker(p, package="hexSticker", p_size=8,
s_x=1, s_y=.75, s_width=1.3, s_height=1,
spotlight=T, filename="ggplot2_highlight.png")
pg$layers = pg$layers[c(1, 3, 2, 4)]
save_sticker('ggplot2_highlight2.png', pg)
Then ggplot2_highlight2.png
will be the first version.
You can remove and/or rearrange any layer except the 1st layer.
Just a minor suggestion @GuangchuangYu : could you please add a parameter to specify the alpha/transparency of the spotlight?
@jotsetung you mean the 0.4
in https://github.com/GuangchuangYu/hexSticker/blob/master/R/sticker.R#L78 ?
yes, right, the max alpha.
All the handcrafted (made with Illustrater) stickers at https://github.com/Bioconductor/BiocStickers have a colour gradient for the background colour. Would it be possible to add this to
hexSticker
as well?