快为你的 R 语言基础绘图系统设定绘图主题吧!

快为你的 R 语言基础绘图系统设定绘图主题吧!

这篇推文是学习 Themes for base plotting system in R 的学习笔记。

basetheme 的魔力就在于,配置好基础绘图系统的主题之后你可能会再次爱上 R 语言的基础绘图系统!

用法

先看一下这个包的基本用法:

1
2
3
4
5
6
7
8
9
10
11
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 主题:

1
2
basetheme("void")
boxplot(split(iris$Sepal.Width, iris$Species))

clean 主题:

1
2
basetheme("clean")
barplot(rivers, col = num2col(rivers))

brutal 主题:

1
2
basetheme("brutal")
plot(hclust(dist(USArrests), "ward.D2"), hang = -1)

royal 主题:

1
2
basetheme("royal")
pairs(iris[-5], bg = lab2col(iris$Species), col = 0)

deepblue 主题:

1
2
basetheme("deepblue")
pairs(iris[-5], bg = num2col(iris[,1]), col = 0)

dark 主题:

1
2
3
4
5
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)

可用主题:

没有设置主题的绘图效果:

1
2
3
4
5
6
7
8
9
10
11
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 主题:

1
2
3
4
5
6
7
8
9
10
11
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 主题:

1
2
3
4
5
6
7
8
9
10
11
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 主题:

1
2
3
4
5
6
7
8
9
10
11
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 主题:

1
2
3
4
5
6
7
8
9
10
11
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 主题:

1
2
3
4
5
6
7
8
9
10
11
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 主题:

1
2
3
4
5
6
7
8
9
10
11
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 主题:

1
2
3
4
5
6
7
8
9
10
11
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 主题:

1
2
3
4
5
6
7
8
9
10
11
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 里面,注意里面的字体设置需要结合你自己电脑上的字体设置进行设置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
pars <- basetheme("default")
pars$palette <- c("#2A363B", "#019875", "#99B898", "#FECEA8", "#FF847C", "#E84A5F", "#C0392B","#96281B")
pars$bg <- "white" # some colors
pars$fg <- "gray20" # some colors
pars$col <- "gray20" # some colors
pars$col.main <- "black" # some colors
pars$col.axis <- "gray20" # some colors
pars$col.lab <- "gray20" # some colors
pars$family <- "CascadiaCode-Regular" # change font
pars$lab <- c(10,10,7) # more ticks on axes
pars$cex.axis <- 0.8 # smaller axis labels
pars$las <- 1 # always horizontal axis labels
pars$rect.border <- "black" # box around the plot
pars$rect.lwd <- 4 # ticker border

basetheme(pars)

barplot(1:9, col = 1:9, names = LETTERS[1:9], main = "barplot", ylab = "heights")

更多内容可以前往 TidyFriday 的知识星球查看:https://t.zsxq.com/MfA6mEu

# R

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×