前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >TidyFriday 快为你的 R 语言基础绘图系统设定绘图主题吧!

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

作者头像
王诗翔呀
发布2020-07-06 17:44:21
6930
发布2020-07-06 17:44:21
举报
文章被收录于专栏:优雅R优雅R

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

❝这篇推文是学习 Themes for base plotting system in R[1] 的学习笔记。 ❞

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

用法

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

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 主题:

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

clean 主题:

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

brutal 主题:

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

royal 主题:

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

deepblue 主题:

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

dark 主题:

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)

可用主题:

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

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 主题:

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 主题:

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 主题:

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 主题:

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 主题:

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 主题:

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 主题:

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 主题:

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 里面,注意里面的字体设置需要结合你自己电脑上的字体设置进行设置:

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

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-02-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 优雅R 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 用法
  • 一些示例
  • 可用主题:
  • 创建自定义主题
    • Reference
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档