Sercan Kahveci

Greetings Mr. Yu,

I am very happy that this package exists. Thank you for making it! I would like to request a feature, to ensure the package is able to compete with professional meme-creation tools like memegenerator and paint.net. Since memes often use the font Impact, in white and with black outline, I believe the package would be more powerful if it also did that automatically.

Regards,

Sercan Kahveci, MSc

Content creator at Questionable Research Memes on Facebook https://www.facebook.com/QResearchMemes/

上次一篇《用R画meme ;)》把脸书在做MEME的人都给炸出来了,专业人士给出专业建议,要用Impact字体,并且要让字体出现阴影效果,这真是厉害了,感觉有点神奇,但他一句,这样就可以和专业工具PK了,着实太刺激了,我决定试一试能不能搞个阴影效果。结果还真让我搞成了,文本有一种浮在上面的错觉。

我们知道meme包是grid写的,我的输出是grob对象,当我半夜调试到2点,带着兴奋入睡之后,一点都睡不安稳,早上早早就迷迷糊糊在床上想着,既然我造出了文本阴影效果的grob对象,我应该封装成geom图层,送给ggplot2用户。

于是我又开始了另一个tiny toy package的开发,shadowtext,顾名思义,出阴影效果的文本。

shadow text grob: shadowtextGrob() and grid.shadowtext()

library(shadowtext)
grid.shadowtext("grid is awesome", gp=gpar(cex=3, col="white"), rot=45)
grid.shadowtext("hello world", y=0.85, gp=gpar(cex=5, col="steelblue"), bg.color="firebrick")
grid.shadowtext("R you ready!!!", y=.1, gp=gpar(cex=4, col="firebrick"))

ggplot2 layer: geom_shadowtext()

ggplot2的成功是站立在grid的肩膀上,有了grid的支持,ggplot2要拿下来也不是很容易,说容易是骗你们的。

geom_shadowtext的使用和geom_text是一样一样的,所以你们不会有使用问题,该怎么用就怎么用,只不过多了bg.color参数用于设置阴影的颜色和bg.r用于设置阴影的大小。

library(ggplot2)

random_text <- function(n=1, length=10) {
    d <- data.frame(n=1:n, length=length)
    sapply(1:nrow(d), function(i) {
        paste(sample(c(0:9, letters, LETTERS),
                     d$length[i], replace=TRUE),
              collapse="")
    })
}

n <- 10
set.seed(2017-10-27)
d <- data.frame(x = rnorm(n), y=rnorm(n),
                label = random_text(n),
                angle = sample(0:360, 10))
p <- ggplot(d, aes(x, y)) + xlim(-2, 2.2) + ylim(-2, 2.4)

p + geom_shadowtext(aes(label=label, angle=angle), size=5)

p + geom_shadowtext(aes(label=label, angle=angle, color=label),
                    bg.color='firebrick', size=5) +
    theme(legend.position="none")

这包已经在CRAN上发布,于是我发现有来自霓虹国的博客文,我已经哭晕,感觉别人出的图比我好看太多了!

你或许觉得这玩意只是玩玩而已,没太多实际应用。我给大家讲一个实际例子,当你画图的背景非常复杂的时候,这个shadowtext包就可以出来非常好的效果,像下面这个只是google map的截屏而已,你可以用ggmap来画,以前你没办法打字像google map这样子的,现在你可以了。