美文网首页
R语言:给图片添加背景

R语言:给图片添加背景

作者: 胡童远 | 来源:发表于2020-07-20 20:03 被阅读0次

导读

参考Y叔的代码试一下在图片里添加文字,不!是给图添加背景。

参考:用R给高考学生送祝福吧

一、选个背景

浙江大学

图片地址:http://pic.baike.soso.com/p/20131107/20131107143406-270169415.jpg

二、制作图片:“大不自多,海纳百川”

R代码:

require(showtext)
require(ggplot2)
require(ggimage)

# font_add_google("Liu Jian Mao Cao", "ziti")
# font_add_google("Alegreya Sans", "ziti")
# font_add_google("Times New Roman", "ziti")
# 需要网络访问,网络不行就算了吧

showtext_auto()

d = data.frame(x=0:1)

p = ggplot(d, aes(x, x)) + geom_blank() +
    annotate("text", x=.45, y=.99,
            label="大不自多",
            family="Times New Roman", size=12, colour="red") +
    annotate("text", x=.45, y=.89,
            label="海纳百川",
            family="Times New Roman", size=12, colour="red") +
    theme_void()
p

此时:

三、给图片加背景

添加背景:

img <- 'http://pic.baike.soso.com/p/20131107/20131107143406-270169415.jpg'
ggbackground(p, img)

截图展示:

结果保存:

result = ggbackground(p, img)
ggsave(result, filename="zju.pdf")
ggsave(result, filename="zju.png")

相关文章

网友评论

      本文标题:R语言:给图片添加背景

      本文链接:https://www.haomeiwen.com/subject/ljcrkktx.html