R 语言 | 公众号封面 Art using R

1. 渐变色与文字

  • https://mp.weixin.qq.com/s/wP4jWxp7MJRdW8vRoI2LzA
  • https://developer.r-project.org/Blog/public/2020/07/15/new-features-in-the-r-graphics-engine/

老消息: 新版R图形引擎支持渐变色了,和PS的距离又进了一点点。

Support for gradient fills, pattern fills, clipping paths and masks has been added to the R graphics engine (in the development version of R, which will probably become R version 4.1.0).

# 在立即执行函数内绘制本期封面图
(function(){
  txt="Immediately \nInvoked Function \nExpression"

  library(grid)

  pdf("./dustbin/20220827.pdf", width=6.8, height=3)
  grid.newpage()
  # bg with linearGradient
  grid.rect(
    gp=gpar(col=NA, fill=linearGradient(
      colours = c("firebrick1", "white")
    ))
  )

  # add text
  grid.text(
    x=0.1,
    label = txt,
    just=c("left", "center"),
    gp=gpar(fontsize=15, cex=2, col="white")
  )
  dev.off()
})()

在这里插入图片描述

2.仿照 office word 顶部界面纹理

主要使用的是 grid 包的基础函数:文字、圆圈、直线。

library(grid)

pdf("BiMooc.logo.pdf", width=518/72, height=196/72)
# 1.背景色 ====
grid.newpage()
grid.rect(gp=gpar(fill="#B7472A"))


# 2.带圆圈的R ====
draw_circ_text=function(txt="R", font_size=10, lwd=8, rot=10){
  g1=textGrob(label = txt, x=0.2, y=0.5,
            #just="right", 
            rot=rot,
            gp=gpar(col="#9A412A", cex=font_size, face="bold")) 
  g2=circleGrob(x=0.2, y=0.5,r=0.30,
              gp=gpar(col="#9A412A", cex=4, lwd=lwd, fill='#00112200'))
  gCircText=gList(g1, g2)
  
  # 当前view画
  grid.draw(gCircText)
}
# 文字1
draw_circ_text("R", 8, 8, 20)

# 文字2
pushViewport(viewport(x=0.7, y = 0.5, height = 0.5, width = 0.5, angle=-30))
#grid.rect( gp=gpar(col="grey", lty=2, fill="#00112200"))
draw_circ_text("R", 4,6, -10)
upViewport()

# 文字3
pushViewport(viewport(x=1, y = 0.3, height = 1.1, width = 0.5))
#grid.rect( gp=gpar(col="grey", lty=2, fill="#00112200"))
draw_circ_text("Python", 3, 4, -40)
upViewport()




# 3. 圆圈开头的电路 ====
## (1) line1 ----
grid.move.to(0.1, 0.9)
grid.line.to(x=0.4, y=0.9,
             gp=gpar(col="#9A412A", fill="#00112200", lwd=4))
# 圆圈1
grid.circle(x=0.1, y=0.9, r=0.02,
            gp=gpar(col="#9A412A", fill="#B7472A", lwd=4))
# 圆圈2
grid.circle(x=0.3, y=0.9, r=0.02,
            gp=gpar(col="#9A412A", fill="#B7472A", lwd=4))
grid.circle(x=0.35, y=0.9, r=0.02,
            gp=gpar(col="#9A412A", fill="#B7472A", lwd=4))
#斜线
grid.line.to(x=0.6, y=0.2,
             gp=gpar(col="#9A412A", fill="#00112200", lwd=4))
# 水平线
grid.line.to(x=1.2, y=0.2,
             gp=gpar(col="#9A412A", fill="#00112200", lwd=4))
# 圆圈3
grid.circle(x=0.65, y=0.2, r=0.02,
            gp=gpar(col="#9A412A", fill="#B7472A", lwd=4))

## (2) line2 ----
grid.move.to(0.32, 0.7)
grid.line.to(x=0.42, y=0.7,
             gp=gpar(col="#9A412A", fill="#00112200", lwd=4))
# 圆圈1
grid.circle(x=0.32, y=0.7, r=0.02,
            gp=gpar(col="#9A412A", fill="#B7472A", lwd=4))
#斜线
grid.line.to(x=0.59, y=0.1,
             gp=gpar(col="#9A412A", fill="#00112200", lwd=4))
# 水平线
grid.line.to(x=1.2, y=0.1,
             gp=gpar(col="#9A412A", fill="#00112200", lwd=4))
# 圆圈3
grid.circle(x=0.62, y=0.1, r=0.02,
            gp=gpar(col="#9A412A", fill="#B7472A", lwd=4))
grid.circle(x=0.75, y=0.1, r=0.02,
            gp=gpar(col="#9A412A", fill="#B7472A", lwd=4))


# 4.导入 pdf  ----
dev.off()

效果图:
在这里插入图片描述

3. QQ群封面图

使用R base 绘图函数 layout 精确布局,画四色R字母圆圈图。

# layout() 布局函数

#pdf("logoR.pdf", width=2.5, height=2.5)
#png("logoR.png", width=2.5, height=2.5, units = "in", res = 72)
#settings
border_width=30
border_color=c("#71BE4B","#ED245B","#FAC319", "#00A1DF")
r=0.85 #radius of circle
#
par(oma=c(0.1,0.1,0.1,0.1)*0) #设置外边界
# 设置为4块,2行,按行排。宽度比1:1,高度比1:1
layout(matrix(c(1:4),nrow=2,byrow=T), widths = c(1,1), heights =c(1,1)) 
for(i in 1:4){
  # 分别在4个块种画空白图
  par(mar=c(0,0,0,0))
  plot(c(0), col='white', xlim=c(-0.5,0.5), ylim=c(-0.5, 0.5), 
       xaxs ="i", yaxs ="i", # 图形与坐标轴不留空白,默认是r
       ann=F, # 不要坐标标签和标题
       axes=F, # 不要坐标和边框
       main="");
  # 接下来依赖循环变量 i,控制圆心位置
  #circle 外圆环
  cir_X=-0.5*(-1)^i
  cir_Y=0.5*ifelse(i<=2, -1, 1)
  # 能画6种形状: circles, squares, rectangles, stars, thermometers, and boxplots
  symbols(x = cir_X, y = cir_Y, circles = r, inches = FALSE, 
          add = TRUE, #画在上一层
          lwd=border_width, #线条宽度
          fg=border_color[i]) #线条颜色
  # text R 内文字
  text(cir_X, cir_Y, labels="R", col=rev(border_color)[i], cex=15, xpd=T)
}

在这里插入图片描述

参考资料

  • https://www.data-imaginist.com/art
  • https://cpastem.caltech.edu/documents/18024/ARt.nb.html
  • https://koenderks.github.io/aRtsy/
  • https://www.thetidytrekker.com/post/thinking-outside-the-grid
  • grid: http://jse.amstat.org/v18n3/zhou.pdf
  • https://www.bigbookofr.com/art.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值