R语言绘图系列:
- R语言可视化及作图1--基础绘图(par函数,散点图,盒形图,条形图,直方图)
- R语言可视化及作图2--低级绘图函数
- R语言可视化及作图3--图形颜色选取
- R语言可视化及作图4--qplot和ggplot2美学函数
- R语言可视化及作图5--ggplot2基本要素和几何对象汇总
- R语言可视化及作图6--ggplot2之点图、条形图、盒形图、直方图、线图
- R语言可视化及作图7--ggplot2之标签、图例和标题绘制
- R语言可视化及作图8--坐标轴自定义和坐标系转换
- R语言可视化及作图9--主题函数
theme函数主要的用途是调节图的主题。如下图,主题主要分为整幅图plot
,坐标轴axis
,图例legend
,面板panel
和分面元素facet
。其中经常用到的是坐标轴主题的修改,比如常见的坐标轴字体大小的修改。
theme函数其实很简单:
theme(主题.部件=element_类型())
主题:plot, axis, legend, panel, facet
部件:title(名字,坐标轴名字), line(线,坐标轴的xy轴), text(标签,坐标轴刻度的数字), ticks(坐标轴刻度的小线条), background(背景)等
类型:rect(所有矩形区域属性),line(所有线属性),text(所有文本相关属性),title(所有标题属性)
说明:部件要和类型一致。比如,部件为title,text等文字相关的元素,那么类型处就为text (具体见上图)。
1. 整幅图的主题设置Plot elements
library(tidyverse)
head(mtcars)
p1=ggplot(mtcars,aes(mpg,disp))+geom_point()
p1+labs(title="xyz")+theme(
plot.background = element_rect(fill = "lightblue", color = "pink", size = 15),
plot.title = element_text(hjust = 1, color = "black", face = "bold",size=15),
plot.margin = margin(t = 30, r = 30, b = 30, l = 30, unit = "pt"))
2. 坐标轴主题设置Axis elements
p1+theme(
axis.line = element_line(color = "lightblue", size = 1),
axis.title = element_text(color = "black", face = "italic"),
axis.ticks = element_line(color = "purple", size = 2),
axis.text = element_text(color = "lightpink"),
axis.text.x = element_text(angle = 45, hjust = 1)
)
3. 面板元素设置
p1+ theme(
panel.background = element_rect(fill = "white", color = "purple"),
panel.grid = element_line(color = "grey80", size = 0.3)
)
4. 图例设置
p2=ggplot(mtcars,aes(mpg,disp,color=cyl))+geom_point()
p2+theme(
legend.background = element_rect(fill = "lightgrey"),
legend.title = element_text(color = "black", size = 15),
legend.key = element_rect(fill = "black"),
legend.text = element_text(color = "blue"),
legend.margin = margin(t = 20, r = 10, b = 10, l = 10, unit = "pt"),
legend.position = "right"
)
5. 参考ggplot2预设主题,绘制自己的theme
ggplot2的预设主题已经写过了,参考R语言可视化及作图9--主题函数。
以theme_linedraw()为例,查看这个主题设置了什么参数
theme_linedraw
## function (base_size = 11, base_family = "", base_line_size = base_size/22,
## base_rect_size = base_size/22)
## {
## half_line <- base_size/2
## theme_bw(base_size = base_size, base_family = base_family,
## base_line_size = base_line_size, base_rect_size = base_rect_size) %+replace%
## theme(axis.text = element_text(colour = "black", size = rel(0.8)),
## axis.ticks = element_line(colour = "black", size = rel(0.5)),
## panel.border = element_rect(fill = NA, colour = "black",
## size = rel(1)), panel.grid = element_line(colour = "black"),
## panel.grid.major = element_line(size = rel(0.1)),
## panel.grid.minor = element_line(size = rel(0.05)),
## strip.background = element_rect(fill = "black"),
## strip.text = element_text(colour = "white", size = rel(0.8),
## margin = margin(0.8 * half_line, 0.8 * half_line,
## 0.8 * half_line, 0.8 * half_line)), complete = TRUE)
## }
## <bytecode: 0x7f818ba6d600>
## <environment: namespace:ggplot2>
它无非是一个具有两个参数的函数:base_size和base_family。其主题部分直接应用的theme函数就是前面写的主题设置函数,这一部分可以直接使用前面介绍的参数进行修改。
网友评论