查看原文
其他

用R画meme ;)

2017-10-27 Y叔 biobabble

Meme最早出现于1976年《The selfish gene》,说的是文化因子一代传一代,与基因连锁,而internet meme,是加了caption的照片,因为搞笑等因素,在网络上像病毒一样传播,所以因此也被叫meme。

在讲座《webinar录播:plotting tree + data》中我有一张slide里面有meme:

这张slide里的东西,基本上是我2014年讲ggplot2时候做的,本公众号回复「ggplot2」可以拿到slides。


这只是其中的一张meme,当时我用了很多meme,那是用memecaption这个web tool做的。在准备完讲座《webinar录播:plotting tree + data》的slides之后,我觉得有必要写一个R包来画meme,因为一直以来,没有R包可以做这个事情,于是就有了这个meme包,虽然只是个很小的包,说白了读张照片,加两句话而已,但依然有我写包的风格,麻雀虽小,五脏俱全,要充分体现出flexibility。如果你想批量给你的图片加水印,它可以做,如果你想写公众号/博客/幻灯片,需要meme图片来搞笑或者衬托一下,你做可以用它来做。

这包还没发布,粉丝已经发来贺电,Y叔又出手了!



I developed a tiny toy package, meme, which is now on CRAN. As it’s name indicated, it was designed to create memes, which are captioned photos that are intended to be funny, riduculous.

meme()

The package is quite simple. You can use meme() function to add meme captions, and this is all the package supposed to do:

library(meme) u <- "http://www.happyfamilyneeds.com/wp-content/uploads/2017/08/angry8.jpg"
meme(u, "code", "all the things!")

the grammar 🙈

The meme package was implemented using grid graphic system. Since grid is the most flexible graphic system in R, I try to mimic ggplot2 (although very superficial) for practice.

User can use mmplot() to read and plot the input image and then using + mm_caption() to add meme captions.

mmplot(u) + mm_caption("calm down", "and RTFM", color="purple")

meme_save()

The meme output can be saved as an object, and can be exported to file using ggsave(). Since we would like to keep the original figure aspect ratio for output meme figure, I provide a helper function, meme_save(), which takes care of the figure aspect ratio and then called ggsave() to export the figure.

u2 <- "http://i0.kym-cdn.com/entries/icons/mobile/000/000/745/success.jpg"
x <- meme(u2, "please", "tell me more") meme_save(x, file="Figs/meme.png")

the plot method

Users can plot the meme() output and change the caption or other parameters in real time.

plot(x, size = 2, "happy friday!", "wait, sorry, it's monday",
    color = "firebrick", font = "Courier")

the + method

Instead of using parameters in plot() explictely, Users can use + aes() to set the plot parameters:

x + aes(upper = "#barbarplots",        lower = "friends don't let friends make bar plots",        color = firebrick, font = Courier, size=1.5)

or using + list(). The following command will also generate the figure displayed above.

x + list(upper = "#barbarplots",        lower = "friends don't let friends make bar plots",        color = "firebrick", font = "Courier", size=1.5)

multi-language support

I didn’t do anything about it. Multi-language was supported internally. Just simply select a font for your language.

y <- meme(u, "卧槽", "听说你想用中文", font="STHeiti") y

As the meme package was developed using grid, It would be better to provide function to convert the output object to grob. Similar to ggplotGrob() for ggplot object, I provide memeGrob() for the meme object and this making it possible to edit the details of the graph and compatible with the grid ecosystem.

Here are the examples of using meme in grid, ggplot2 and cowplot.

grid support

library(grid) mm <- meme(u, "code", "all the things!", size=.3, color='firebrick') grid.newpage() pushViewport(viewport(width=.9, height=.9)) grid.rect(gp = gpar(lty="dashed")) xx <- seq(0, 2*pi , length.out=10) yy <- sin(xx)for (i in seq_along(xx)) {    vp <- viewport(x = xx[i]/(2*pi), y = (yy[i]-min(yy))/2, width=.05, height=.05)    print(mm, vp = vp) }

ggplot2 support

library(ggplot2)
library(ggimage) d <- data.frame(x = xx, y = yy) ggplot(d, aes(x, y)) + geom_line() +    geom_subview(mm, x = xx, y = yy, width=.3, height=.15)

ggplot(d, aes(x, y)) +    geom_subview(mm+aes(size=3), x=0, y=0, width=Inf, height=Inf) +    geom_point() + geom_line()

cowplot support

cowplot::plot_grid(x, y, ncol=1, labels = c("A", "B"))


考虑到英文的,一般人不会看完,看到这里的才是真爱粉,特意为你们送上时光机:《有魔性的图来一发》。


这个包发布之后,立刻有人用起来,有个叫Archer的成人卡通情景喜剧,有人就去截屏,屏幕上有台词嘛,然后用meme包,给它在上面配上个对白,真是有想法,干得漂亮!


就在我写好了这篇文章之后,我又双叒叕加了新功能,除了改变默认字体之外,让字打上去有阴影效果!!!有了这效果,这包做MEME已经不输给任何其它的工具了!

您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存