本内容为【科研私家菜】R可视化之美之科研绘图系列课程,快来收藏关注【科研私家菜】
01 R语言表达式
在R语言中,狭义的表达式指表达式(expression)类对象,由expression函数产生;而广义的的表达式既包含expression类,也包含R“语言”类(language)。expression和language是R语言中两种特殊数据类:
getClass(“expression”)
getClass(“language”)
y <- function(x) (exp(-(x^2)/2))/sqrt(2*pi)
plot(y, -5, 5, main = expression(f(x) == frac(1,sqrt(2*pi))*e^(-frac(x^2,2))), lwd = 3, col = "blue")
效果如下:

library(ggplot2)
x <- seq(0, 2*pi, by = 0.01)
y <- sin(x)
data <- data.frame(x, y)
p <- ggplot(data, aes(x, y)) + geom_line()
p + geom_area(fill = 'blue', alpha = 0.3) +
scale_x_continuous(breaks = c(0, pi, 2*pi), labels = c('0', expression(pi), expression(2*pi))) +
geom_text(parse = T, aes(x = pi/2,y = 0.3, label = 'integral(sin(x)*dx, 0, pi)'))
效果如下:

02 expression 函数
expression函数可以有一个或多个参数,它把全部参数当成一个列表,每个参数都被转成一个表达式向量,所以它的返回值是表达式列表,每个元素都是表达式类型对象,返回值的长度等于参数的个数:
(ex <- expression(x = 1, 1 + sqrt(a)))
## expression(x = 1, 1 + sqrt(a))
length(ex)
## [1] 2
ex[1]
## expression(x = 1)
mode(ex[1])
## [1] "expression"
typeof(ex[1])
## [1] "expression"
ex[2]
## expression(1 + sqrt(a))
mode(ex[2])
## [1] "expression"
typeof(ex[2])
## [1] "expression"
03 quote函数
quote函数只能有一个参数。quote函数的返回值一般情况下是call类型,表达式参数是单个变量的话返回值就是name类型,如果是常量那么返回值的存储模式就和相应常量的模式相同:
(cl <- quote(1 + sqrt(a) + b^c))
## 1 + sqrt(a) + b^c
mode(cl)
## [1] "call"
typeof(cl)
## [1] "language"
(cl <- quote(a))
## a
mode(cl)
## [1] "name"
typeof(cl)
## [1] "symbol"
(cl <- quote(1))
## [1] 1
mode(cl)
## [1] "numeric"
typeof(cl)
## [1] "double"
length(quote(a)) #name或常量类型,返回值长度为1
## [1] 1
length(quote(!a)) #单目运算符,返回值长度为2
## [1] 2
length(quote(-b)) #单目运算符,返回值长度为2
## [1] 2
length(quote(a + b)) #双目运算符,返回值长度为3
## [1] 3
length(quote((a + b) * c)) #多个运算符只算优先级最低的一个
## [1] 3
04 bquote 和 substitute 函数
如果不使用环境变量或环境变量参数,bquote 和 substitute 函数得到的结果与quote函数相同。
bquote(1 + sqrt(a) + b^c) == quote(1 + sqrt(a) + b^c)
## [1] TRUE
substitute(1 + sqrt(a) + b^c) == quote(1 + sqrt(a) + b^c)
## [1] TRUE
par(mar = rep(0.1, 4), cex = 2)
plot.new()
plot.window(c(0, 10), c(0, 1))
for (i in 1:9) text(i, 0.5, substitute(sqrt(x, a), list(a = i + 1)))

05 parse 函数
parse函数用于从文件读取文本作为表达式,返回的值是expression类型。
x <- 1
x + "x"
## Error: 二进列运算符中有非数值参数
expression(x + "x")
## expression(x + "x")
quote(x + "x")
## x + "x"
expression(x + +++y)
## expression(x + +++y)
expression(x - ---y)
## expression(x - ---y)
## expression(x****y) (Not run) expression(xy) (Not run)
## expression(1<=x<=4) (Not run)
quote(x + +++y)
## x + +++y
quote(x - ---y)
## x - ---y
## quote(x****y) (Not run) quote(xy) (Not run) quote(1<=x<=4) (Not run)
- 运算连续使用不出错是因为它们还可以当成求正/负值运算的符号。 在表达式产生函数中使用paste函数可以解决这样的问题。在这种条件下,paste对参数的处理方式和表达式产生函数一样,检查运算符但不检查变量名。用NULL作为运算符的参数可以获得意外的效果:
ex <- expression(paste(x, "", y))
cl <- quote(paste(x, "****", y))
par(mar = rep(0.1, 4), cex = 2)
plot.new()
plot.window(c(0, 1.2), c(0, 1))
text(0.2, 0.5, ex)
text(0.6, 0.5, cl)
cl <- quote(paste(1 <= x, NULL <= 4))
text(1, 0.5, cl)

quote, bquote 和 substitute 的返回值有三种类型call, name 和 常量,事实上expression 函数的结果最终也是这三种类型。因为expression函数的结果是expression列表,我们取列表元素的值检查看看:
(ex <- expression(1 + sqrt(x), x, 1))
## expression(1 + sqrt(x), x, 1)
ex[[1]]
## 1 + sqrt(x)
mode(ex[[1]])
## [1] "call"
typeof(ex[[1]])
## [1] "language"
ex[[2]]
## x
mode(ex[[2]])
## [1] "name"
typeof(ex[[2]])
## [1] "symbol"
ex[[3]]
## [1] 1
mode(ex[[3]])
## [1] "numeric"
typeof(ex[[3]])
## [1] "double"
ex <- expression(sqrt(x), x + y, x^2, x %in% A, x <= y, mean(x, y, z), x | y, x & y)
n <- length(ex)
par(mar = rep(0.1, 4), cex = 1.5)
col <- c("red", "blue")
plot.new()
plot.window(c(0, n), c(0, 1))
for (i in 1:n) text(i - 0.5, 0.5, ex[i], col = col[i%%2 + 1])

最后附一张参考公式图

参考资料
关注R小盐,关注科研私家菜(溦❤工众號: SciPrivate),有问题请联系R小盐。让我们一起来学习 R可视化之美之科研绘图
网友评论