快为你的 R 语言基础绘图系统设定绘图主题吧!
❝这篇推文是学习 Themes for base plotting system in R[1] 的学习笔记。 ❞
basetheme
的魔力就在于,配置好基础绘图系统的主题之后你可能会再次爱上 R 语言的基础绘图系统!
用法
先看一下这个包的基本用法:
代码语言:javascript复制library(basetheme)
basetheme(pch = 19, mgp = c(2, 0.7, 0), tck = -0.01)
# 根据名称选择主题
basetheme("clean")
basetheme("minimal", bg = "grey", pch = 1)
# basetheme("clean") 会返回一个 list,可以通过下面的方式对其中的设置进行微调
theme <- basetheme("clean")
theme$cex.main <- 2
basetheme(theme)
# 取消主题设置
basetheme(NULL)
一些示例
void 主题:
代码语言:javascript复制basetheme("void")
boxplot(split(iris$Sepal.Width, iris$Species))
clean 主题:
代码语言:javascript复制basetheme("clean")
barplot(rivers, col = num2col(rivers))
brutal 主题:
代码语言:javascript复制basetheme("brutal")
plot(hclust(dist(USArrests), "ward.D2"), hang = -1)
royal 主题:
代码语言:javascript复制basetheme("royal")
pairs(iris[-5], bg = lab2col(iris$Species), col = 0)
deepblue 主题:
代码语言:javascript复制basetheme("deepblue")
pairs(iris[-5], bg = num2col(iris[,1]), col = 0)
dark 主题:
代码语言:javascript复制x <- seq(-1.95, 1.95, length.out = 30)
y <- seq(-1.95, 1.95, length.out = 35)
z <- outer(x, y, function(a, b) a * b ^ 2)
basetheme("dark")
persp(x, y, z, theta = -45)
可用主题:
没有设置主题的绘图效果:
代码语言:javascript复制basetheme("default")
pairs(iris[, 1:4], col = lab2col(iris$Species))
legend("bottom",
legend = unique(iris$Species),
col = lab2col(unique(iris$Species)),
pch = par("pch"),
cex = 0.8,
horiz = TRUE,
bty = "n",
inset = c(0, 1),
xpd = TRUE)
clean 主题:
代码语言:javascript复制basetheme("clean")
pairs(iris[, 1:4], col = lab2col(iris$Species))
legend("bottom",
legend = unique(iris$Species),
col = lab2col(unique(iris$Species)),
pch = par("pch"),
cex = 0.8,
horiz = TRUE,
bty = "n",
inset = c(0, 1),
xpd = TRUE)
brutal 主题:
代码语言:javascript复制basetheme("brutal")
pairs(iris[, 1:4], col = lab2col(iris$Species))
legend("bottom",
legend = unique(iris$Species),
col = lab2col(unique(iris$Species)),
pch = par("pch"),
cex = 0.8,
horiz = TRUE,
bty = "n",
inset = c(0, 1),
xpd = TRUE)
ink 主题:
代码语言:javascript复制basetheme("ink")
pairs(iris[, 1:4], col = lab2col(iris$Species))
legend("bottom",
legend = unique(iris$Species),
col = lab2col(unique(iris$Species)),
pch = par("pch"),
cex = 0.8,
horiz = TRUE,
bty = "n",
inset = c(0, 1),
xpd = TRUE)
dark 主题:
代码语言:javascript复制basetheme("dark")
pairs(iris[, 1:4], col = lab2col(iris$Species))
legend("bottom",
legend = unique(iris$Species),
col = lab2col(unique(iris$Species)),
pch = par("pch"),
cex = 0.8,
horiz = TRUE,
bty = "n",
inset = c(0, 1),
xpd = TRUE)
deepblue 主题:
代码语言:javascript复制basetheme("deepblue")
pairs(iris[, 1:4], col = lab2col(iris$Species))
legend("bottom",
legend = unique(iris$Species),
col = lab2col(unique(iris$Species)),
pch = par("pch"),
cex = 0.8,
horiz = TRUE,
bty = "n",
inset = c(0, 1),
xpd = TRUE)
royal 主题:
代码语言:javascript复制basetheme("royal")
pairs(iris[, 1:4], col = lab2col(iris$Species))
legend("bottom",
legend = unique(iris$Species),
col = lab2col(unique(iris$Species)),
pch = par("pch"),
cex = 0.8,
horiz = TRUE,
bty = "n",
inset = c(0, 1),
xpd = TRUE)
minimal 主题:
代码语言:javascript复制basetheme("minimal")
pairs(iris[, 1:4], col = lab2col(iris$Species))
legend("bottom",
legend = unique(iris$Species),
col = lab2col(unique(iris$Species)),
pch = par("pch"),
cex = 0.8,
horiz = TRUE,
bty = "n",
inset = c(0, 1),
xpd = TRUE)
void 主题:
代码语言:javascript复制basetheme("void")
pairs(iris[, 1:4], col = lab2col(iris$Species))
legend("bottom",
legend = unique(iris$Species),
col = lab2col(unique(iris$Species)),
pch = par("pch"),
cex = 0.8,
horiz = TRUE,
bty = "n",
inset = c(0, 1),
xpd = TRUE)
创建自定义主题
这个主题就是我现在自己使用的主题,可以放进 R Profile 里面,注意里面的字体设置需要结合你自己电脑上的字体设置进行设置:
代码语言:javascript复制pars <- basetheme("default")
pars$palette <- c("#2A363B", "#019875", "#99B898", "#FECEA8", "#FF847C", "#E84A5F", "#C0392B","#96281B")
pars$bg <- "white"
pars$fg <- "gray20"
pars$col <- "gray20"
pars$col.main <- "black"
pars$col.axis <- "gray20"
pars$col.lab <- "gray20"
pars$family <- "CascadiaCode-Regular" # 字体
pars$lab <- c(10,10,7)
pars$las <- 1
pars$rect.border <- "black"
pars$rect.lwd <- 4
basetheme(pars)
barplot(1:9, col = 1:9, names = LETTERS[1:9], main = "barplot", ylab = "heights")
Reference
[1]
Themes for base plotting system in R:https://github.com/KKPMW/basetheme