本笔记为个人学习整理(图文版),补充了部分内容,仅供参考。原内容在文末附有链接。版权归原作者所有,如有侵权,请联系删除。
Hello!大家好呀,今天主要来学习ggplot2
绘图包的theme
参数(~)!
在数据可视化过程中,图形的美观和易读性至关重要。ggplot2
作为 R
语言中强大的绘图工具,提供了丰富的自定义选项,让我们能够轻松创建专业的图表。而其中,theme
功能尤为重要。通过合理的 theme
设置,我们可以调整图表的背景、网格线、坐标轴、文本等元素,使图表更加美观和易于解读。
之前的一些基础笔记嗷:
代码注释由GPT产生
主题系统
交换x轴和y轴
使用coord_flip()
直接交换即可
library(ggplot2)
library(tibble)
library(patchwork)
library(RColorBrewer)
set.seed(123)
data <- tibble(
group = rep(c("A", "B", "C"), each = 100),
value1 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2)),
value2 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2)),
value3 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2))
)
colorgroup <- brewer.pal(3, "Set3")
p1 <- ggplot(data, aes(x = group, y = value1, fill = group)) +
geom_violin() +
geom_boxplot(width = 0.1, fill = "white", color = "black", alpha = 0.5) +
scale_fill_manual(values = colorgroup) +
theme_classic() +
labs(x = "Value", y = "Group", title = "Violin Plot - Value1")
p2 <- p1 + coord_flip()
p1 + p2
修改x轴和y轴范围
使用的是xlim()和ylim()
。这两种方式scale_x_continuous()
和scale_y_continuous()
的简便写法。
library(ggplot2)
library(tibble)
library(patchwork)
library(RColorBrewer)
set.seed(123)
data <- tibble(
group = rep(c("A", "B", "C"), each = 100),
value1 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2)),
value2 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2)),
value3 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2))
)
colorgroup <- brewer.pal(3, "Set3")
p1 <- ggplot(data, aes(x = group, y = value1, fill = group)) +
geom_violin() +
geom_boxplot(width = 0.1, fill = "white", color = "black", alpha = 0.5) +
scale_fill_manual(values = colorgroup) +
theme_classic() +
labs(x = "Value", y = "Group", title = "Violin Plot - Value1")
p2 <- p1 + ylim(-3,5)
p1 + p2
修改x轴和y轴范围及断点位置
前面讲到修改x轴和y轴范围使用的是xlim()和ylim()
。但这两个无法自定义断点。要自定义断点则需使用scale_x_continuous()和scale_y_continuous()
以scale_y_continuous()
为例:
scale_y_continuous(limits = c(min, max))
:定义了范围,和ylim
一样scale_y_continuous(breaks = seq(-3, 5, by = 1))
:指定范围,设置步长为1
scale_y_continuous(breaks = c(-3, 1, 2, 5))
:自定义断点向量
library(ggplot2)
library(tibble)
library(patchwork)
library(RColorBrewer)
colorgroup <- brewer.pal(3, "Set3")
set.seed(123)
data <- tibble(
group = rep(c("A", "B", "C"), each = 100),
value1 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2)),
value2 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2)),
value3 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2))
)
p1 <- ggplot(data, aes(x = group, y = value1, fill = group)) +
geom_violin() +
geom_boxplot(width = 0.1, fill = "white", color = "black", alpha = 0.5) +
scale_fill_manual(values = colorgroup) +
theme_classic() +
labs(x = "Value", y = "Group", title = "Violin Plot - Value1")
p2 <- p1 + scale_y_continuous(limits = c(-3,6))
p3 <- p2 + scale_y_continuous(breaks = seq(-3, 5, by = 1))
p4 <- p3 + scale_y_continuous(breaks = c(-3, 1, 2, 5))
p2 | p3 | p4
反转x轴和y轴范围
在某些特殊情况下,需要将坐标轴的范围顺序逆转。这里通常使用scale_y_reverse 或 scale_x_reverse
具体使用方法和上述相同
library(ggplot2)
library(tibble)
library(patchwork)
library(RColorBrewer)
colorgroup <- brewer.pal(3, "Set3")
set.seed(123)
data <- tibble(
group = rep(c("A", "B", "C"), each = 100),
value1 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2)),
value2 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2)),
value3 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2))
)
p1 <- ggplot(data, aes(x = group, y = value1, fill = group)) +
geom_violin() +
geom_boxplot(width = 0.1, fill = "white", color = "black", alpha = 0.5) +
scale_fill_manual(values = colorgroup) +
theme_classic() +
labs(x = "Value", y = "Group", title = "Violin Plot - Value1")
p2 <- p1 + ylim(6,-3)
p3 <- p2 + scale_y_reverse(breaks = seq(6, -3, by = -1.5))
p1 | p2 | p3
修改类别型坐标轴项目顺序
通过设定 scale_x_discrete() 或 scale_y_discrete()
中的参数 limits
来修改
library(ggplot2)
library(tibble)
library(patchwork)
library(RColorBrewer)
colorgroup <- brewer.pal(3, "Set3")
set.seed(123)
data <- tibble(
group = rep(c("A", "B", "C"), each = 100),
value1 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2)),
value2 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2)),
value3 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2))
)
p1 <- ggplot(data, aes(x = group, y = value1, fill = group)) +
geom_violin() +
geom_boxplot(width = 0.1, fill = "white", color = "black", alpha = 0.5) +
scale_fill_manual(values = colorgroup) +
theme_classic() +
labs(x = "Value", y = "Group", title = "Violin Plot - Value1")
p2 <- p1 + scale_x_discrete(limits = c("B", "C"))
p1 | p2
如果反转顺序且x
轴项目过多时,可以使用scale_x_discrete(limits = c("A","B","C"))
。同时可以搭配breaks用来控制显示哪像项目拥有标签,scale_x_discrete(limits = c("A","B","C"), breaks = c("A","C"))
library(ggplot2)
library(tibble)
library(patchwork)
library(RColorBrewer)
colorgroup <- brewer.pal(3, "Set3")
set.seed(123)
data <- tibble(
group = rep(c("A", "B", "C"), each = 100),
value1 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2)),
value2 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2)),
value3 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2))
)
p1 <- ggplot(data, aes(x = group, y = value1, fill = group)) +
geom_violin() +
geom_boxplot(width = 0.1, fill = "white", color = "black", alpha = 0.5) +
scale_fill_manual(values = colorgroup) +
theme_classic() +
labs(x = "Value", y = "Group", title = "Violin Plot - Value1")
p2 <- p1 +scale_x_discrete(limits = c("A","B","C"), breaks = c("A","C"))
p1 | p2
设置x轴和y轴缩放比例
这个其实和设置修改x
轴和y
轴范围及断点有点类似,这里我们使用散点图举一个例子。
library(ggplot2)
library(patchwork)
data <- data.frame(
x = 1:10,
y = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
)
p1 <- ggplot(data, aes(x = x, y = y)) +
geom_point(colour = "skyblue",size = 3) +
coord_fixed(ratio = 1) +
theme_classic()
p2 <- p1 + coord_fixed(ratio = 1.5/1)+
scale_x_continuous(breaks = (seq(0, 10, by = 2)))
p1 | p2
coord_fixed(ratio = 1.5/1)
表示y
轴是x
轴的1.5
倍
移除刻度线和标签
在ggplot2
中,刻度线和标签是由axis
参数来控制。
library(ggplot2)
library(tibble)
library(patchwork)
library(RColorBrewer)
colorgroup <- brewer.pal(3, "Set3")
set.seed(123)
data <- tibble(
group = rep(c("A", "B", "C"), each = 100),
value1 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2)),
value2 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2)),
value3 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2))
)
p1 <- ggplot(data, aes(x = group, y = value1, fill = group)) +
geom_violin() +
geom_boxplot(width = 0.1, fill = "white", color = "black", alpha = 0.5) +
scale_fill_manual(values = colorgroup) +
theme_classic() +
labs(x = "Value", y = "Group", title = "Violin Plot - Value1")
p2 <- p1 + theme(
axis.text.x = element_blank(),
axis.line.x = element_blank(),
axis.ticks.x = element_blank(),
)
p1 | p2
根据参数的名称即可知道,axis.text.x = element_blank()
,表示x
轴的文本等于元素空白。同理,y
轴的标签文本也可通过axis.text.t = element_blank()
。axis.line.x = element_blank()
表示移除x
轴的线(最长的那条线)。axis.ticks.x = element_blank()
表示移除刻度短线。所有对应的y
轴是同样的操作。如果要同时移除x
轴和y
轴,即可参数定位至二级参即可,如axis.text
。
需要注意的是,
axis位于theme
内
scale_y_continuous(breaks=NULL)
允许我们来删除y
轴相关的所有轴参数,包括:y
轴、y
轴文本、y
轴刻度短线、y
方向的图形背景网格线。例如:
library(ggplot2)
library(tibble)
library(patchwork)
library(RColorBrewer)
colorgroup <- brewer.pal(3, "Set3")
set.seed(123)
data <- tibble(
group = rep(c("A", "B", "C"), each = 100),
value1 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2)),
value2 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2)),
value3 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2))
)
p1 <- ggplot(data, aes(x = group, y = value1, fill = group)) +
geom_violin() +
geom_boxplot(width = 0.1, fill = "white", color = "black", alpha = 0.5) +
scale_fill_manual(values = colorgroup) +
labs(x = "Value", y = "Group", title = "Violin Plot - Value1")
p2 <- p1 + scale_y_continuous(breaks = NULL)+
theme_classic()
p1 | p2
修改刻度标签的文本
上述提到使用axis.text
参数来控制双轴的文本显示。使用scale
参数控制了刻度值距离与整个轴的参数控制。那么当修改双轴的刻度文本标签和内容时,也是通过该参数来控制的。
library(ggplot2)
library(tibble)
library(patchwork)
library(RColorBrewer)
colorgroup <- brewer.pal(3, "Set3")
set.seed(123)
data <- tibble(
group = rep(c("A", "B", "C"), each = 100),
value1 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2)),
value2 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2)),
value3 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2))
)
p1 <- ggplot(data, aes(x = group, y = value1, fill = group)) +
geom_violin() +
geom_boxplot(width = 0.1, fill = "white", color = "black", alpha = 0.5) +
scale_fill_manual(values = colorgroup) +
labs(x = "Value", y = "Group", title = "Violin Plot - Value1")
p2 <- p1 +
ylim(-3, 10)+
scale_y_continuous(limits = c(-3, 10), breaks = c(-3,0,3,6,9), labels = c("这是-3","这是0","这是3","这是6","这是9"))+
theme_classic()
p1 | p2
scale_y_continuous(breaks = c(-3,0,3,6,9))
参数仅在图形数据所在范围内可定义断点。如果要强行修改值域范围,则需要scale_y_continuous(limits = c(-3, 10), breaks = c(-3,0,3,6,9), labels = c("这是-3","这是0","这是3","这是6","这是9"))
。limits用于定义新的值域范围,breaks
定义断点,labels
用于修改断点标签文本。
修改刻度标签的外观
修改刻度标签的外观,需要在theme
内的axis.text
里来修改
library(ggplot2)
library(tibble)
library(patchwork)
library(RColorBrewer)
colorgroup <- brewer.pal(3, "Set3")
set.seed(123)
data <- tibble(
group = rep(c("A", "B", "C"), each = 100),
value1 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2)),
value2 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2)),
value3 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2))
)
p1 <- ggplot(data, aes(x = group, y = value1, fill = group)) +
geom_violin() +
geom_boxplot(width = 0.1, fill = "white", color = "black", alpha = 0.5) +
scale_fill_manual(values = colorgroup) +
labs(x = "Value", y = "Group", title = "Violin Plot - Value1")
p2 <- p1 +
scale_y_continuous(limits = c(-3, 10), breaks = c(-3,0,3,6,9), labels = c("这是-3","这是0","这是3","这是6","这是9"))+
theme(
axis.text.y = element_text(angle = 90, hjust = 0.5, vjust = 1)
)
p1 | p2
angle
参数用于设置文本的旋转角度,通常范围为-90到90度
。当角度为正时,文本逆时针旋转;当角度为负时,文本顺时针旋转。
hjust
参数用于设置文本水平对齐方式,其范围为0
到1
。当hjust = 0
时,文本左对齐;当hjust = 0.5
时,文本居中对齐;当hjust = 1
时,文本右对齐。
vjust
参数用于设置文本垂直对齐方式,其范围为0
到1
。当vjust = 0
时,文本底部对齐;当vjust = 0.5
时,文本垂直居中对齐;当vjust = 1
时,文本顶部对齐。
除了基本的修改外,还支持修改字体,大小,样式等
修改坐标轴标签的文本
坐标轴标签文本实际就是我们通常所说的x
轴标题和y
轴标题,使用xlab和ylab
修改即可
当然,也支持其他的修改方式,但这种比较简便。推荐使用这种。
移除坐标轴标签的文本
移除坐标轴标签文本使用的函数时axis.title
library(ggplot2)
library(tibble)
library(patchwork)
library(RColorBrewer)
colorgroup <- brewer.pal(3, "Set3")
set.seed(123)
data <- tibble(
group = rep(c("A", "B", "C"), each = 100),
value1 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2)),
value2 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2)),
value3 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2))
)
p1 <- ggplot(data, aes(x = group, y = value1, fill = group)) +
geom_violin() +
geom_boxplot(width = 0.1, fill = "white", color = "black", alpha = 0.5) +
scale_fill_manual(values = colorgroup) +
labs(x = "Value", y = "Group", title = "Violin Plot - Value1")
p2 <- p1 +
theme(
axis.title.x = element_blank()
)
p1 | p2
沿坐标轴显示直线
这里讨论的是不应用任何预置主题样式显示坐标轴直线的情况
library(ggplot2)
library(tibble)
library(patchwork)
library(RColorBrewer)
colorgroup <- brewer.pal(3, "Set3")
set.seed(123)
data <- tibble(
group = rep(c("A", "B", "C"), each = 100),
value1 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2)),
value2 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2)),
value3 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2))
)
p1 <- ggplot(data, aes(x = group, y = value1, fill = group)) +
geom_violin() +
geom_boxplot(width = 0.1, fill = "white", color = "black", alpha = 0.5) +
scale_fill_manual(values = colorgroup) +
labs(x = "Value", y = "Group", title = "Violin Plot - Value1")
p2 <- p1 +
theme(
axis.line = element_line(colour = "black")
)
p1 | p2
element_line
参数内可通过size
定义线条粗细程度。
使用对数坐标轴
使用scale_x_log10()
和/或scale_y_log10()
library(ggplot2)
library(tibble)
library(patchwork)
library(RColorBrewer)
library(scales)
colorgroup <- brewer.pal(3, "Set3")
set.seed(123)
data <- tibble(
group = rep(c("A", "B", "C"), each = 100),
value1 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2)),
value2 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2)),
value3 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2))
)
p1 <- ggplot(data, aes(x = group, y = value1, fill = group)) +
geom_violin() +
geom_boxplot(width = 0.1, fill = "white", color = "black", alpha = 0.5) +
scale_fill_manual(values = colorgroup) +
labs(x = "Value", y = "Group", title = "Violin Plot - Value1")
p2 <- p1 +
scale_y_log10(breaks = seq(-3, 10, by = 1),labels = trans_format("log10", math_format(10^.x)))
p1 | p2
limits
定义了y
轴的范围,将参数值传递给trans_format
,意为转换为log10
为底的数,math_format
指定了labels
的格式而已。可以通过breaks
参数自定义断点然后进行转化。
需要注意的是,如果breaks是c(1,10,100)
那么转换为对应的就是0,1,2
。这个间距在转换时需要考虑好,否则结果类似于图4
设置图形标题
设置图形的标题可以使用ggtitle()
或labs(title = "")
。\n
表示换行。如果需要调整位置,可以在theme
内使用plot.title
配合vjust
和hjust
使用
library(ggplot2)
library(tibble)
library(patchwork)
library(RColorBrewer)
colorgroup <- brewer.pal(3, "Set3")
set.seed(123)
data <- tibble(
group = rep(c("A", "B", "C"), each = 100),
value1 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2)),
value2 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2)),
value3 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2))
)
p1 <- ggplot(data, aes(x = group, y = value1, fill = group)) +
geom_violin() +
geom_boxplot(width = 0.1, fill = "white", color = "black", alpha = 0.5) +
scale_fill_manual(values = colorgroup) +
labs(x = "Value", y = "Group", title = "Violin Plot - Value1")
p2 <- p1 +
ggtitle("the plot title") +
labs(title = "the plot title2") +
xlab("x -labs") +
theme(
plot.title = element_text(hjust = .5)
)
p1 | p2
修改文本外观
坐标轴的标签文本和坐标轴标题的文本可使用axis.text
和theme
中plot.title
中的element_text()
参数。
要设置文本几何对象,这里的文本对象指的是通过geom_text()
或者annotate()
添加的文本
library(ggplot2)
library(tibble)
library(patchwork)
library(RColorBrewer)
colorgroup <- brewer.pal(3, "Set3")
set.seed(123)
data <- tibble(
group = rep(c("A", "B", "C"), each = 100),
value1 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2)),
value2 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2)),
value3 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2))
)
p1 <- ggplot(data, aes(x = group, y = value1, fill = group)) +
geom_violin() +
geom_boxplot(width = 0.1, fill = "white", color = "black", alpha = 0.5) +
scale_fill_manual(values = colorgroup) +
labs(x = "Value", y = "Group", title = "Violin Plot - Value1")
p2 <- p1 +
ggtitle("the plot title") +
geom_text(label = "this is a text", x = "B", y = 3, colour = "skyblue")
p1 | p2
geom_text
的用法和其他geom
函数一样,通过aes
进行传递。aes
函数需要传递x和y
,用于定位文本标签位置,label
指定文本内容。还可以设置文本标签的样式属性,如大小、颜色和对齐方式
此外,还可以通过annotate
来添加文本。annotate()
函数允许您直接在图形中添加注释,而不需要依赖于数据框。您可以指定文本的位置、内容和样式属性,从而在图中添加自定义的注释。这种方法特别适用于添加静态文本或标签,而不是基于数据的标签。
主题元素和文本几何对象的文本属性
主题元素 | 文本几何对象 | 说明 |
---|---|---|
family | label | 设置文本的字体系列(例如 "sans" , "serif" , "mono" ) |
face | label | 设置文本的字体样式(例如 "plain" , "italic" , "bold" ) |
colour | label | 设置文本的颜色 |
size | label | 设置文本的大小 |
hjust | geom_text、geom_label | 设置文本的水平对齐方式(0表示左对齐,0.5表示居中,1表示右对齐) |
vjust | geom_text、geom_label | 设置文本的垂直对齐方式(0表示底部对齐,0.5表示居中,1表示顶部对齐) |
angle | label | 设置文本的旋转角度 |
lineheight | label | 设置文本的行高 |
theme中控制文本外观的项目
元素名称 | 说明 |
---|---|
axis.title | 坐标轴标题 |
axis.title.x | X轴标题 |
axis.title.y | Y轴标题 |
axis.ticks | 坐标轴刻度线 |
axis.ticks.x | X轴刻度线 |
axis.ticks.y | Y轴刻度线 |
plot.title | 绘图标题 |
legend.title | 图例标题 |
legend.text | 图例标签 |
strip.text | 分面(facet)标签 |
strip.text.x | X轴分面标签 |
strip.text.y | Y轴分面标签 |
使用默认主题
ggplot2
内置的几种常用的主题:
主题 | 说明 |
---|---|
theme_minimal | 简约风格,去除了背景灰色和网格线,适用于简洁的图形展示 |
theme_classic | 经典风格,带有灰色背景和白色网格线,适用于传统的图形呈现 |
theme_void | 空白主题,没有背景和网格线,适用于需要自定义所有图形元素的情况 |
theme_light | 浅色主题,带有浅色背景和灰色网格线,适用于需要更轻盈的视觉效果的情况 |
theme_dark | 深色主题,带有深色背景和浅色网格线,适用于需要更显眼的图形展示 |
theme_linedraw | 简笔画风格,带有黑色网格线和简单的线条图形,适用于手绘风格的图形呈现 |
theme_minimalist | 极简主题,去除了大部分的图形元素,适用于需要极简风格的图形展示 |
修改主题元素的外观
要修改一套主题的元素内容,仅需在theme
内新添加参数即可。
上述学习了axis.text/title/和plot.title/text
,这些外观通常只涉及文本和线条。特殊情况下,需要对绘图主题区域进项修改。主要涉及:
panel.grid.minor
:次要网格线的属性,例如颜色、线型等。panel.border
:面板边框的属性,例如颜色、线型等。panel.spacing
:控制面板内部元素之间的间距。panel.margin
:控制面板周围的边距大小。panel.background
:面板背景的属性,例如填充颜色、边框颜色等。
上述的参数主要涉及:element_line/rect
另一大板块则为图例:
legend.title
:图例标题的属性,例如文本内容、颜色、字体等。legend.text
:图例标签的属性,例如文本内容、颜色、字体等。legend.position
:图例的位置,例如左上角、右下角等。"left"(左侧)、"right"(右侧)、"bottom"(下方)、"top"(上方),还可以通过坐标将图例置于绘图区域内部(左下角为c(0,0),右上角为c(1,1)) 若希望放置在绘图区域内部,则需指定一个双元素数值向量legend.direction
:图例的排列方向,例如水平、垂直等。legend.key
:图例标识(key)的属性,例如形状、颜色、大小等。
element_blank()
表示对应元素为空白
legend.position
和legend.justification
自定义图例位置。position
的四个位置参数可控制图例位于图形外侧的位置。当position
为坐标时,只时定义了图例的中心位置位于position
所指定的坐标,配合justification
才能精确定位。
library(ggplot2)
library(tibble)
library(patchwork)
library(RColorBrewer)
colorgroup <- brewer.pal(3, "Set3")
set.seed(123)
data <- tibble(
group = rep(c("A", "B", "C"), each = 100),
value1 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2)),
value2 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2)),
value3 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2))
)
p1 <- ggplot(data, aes(x = group, y = value1, fill = group)) +
geom_violin() +
geom_boxplot(width = 0.1, fill = "white", color = "black", alpha = 0.5) +
scale_fill_manual(values = colorgroup) +
labs(x = "Value", y = "Group", title = "Violin Plot - Value1") +
theme(
legend.position = c(1,0)
)
p2 <- p1 +
ggtitle("the plot title") +
geom_text(label = "this is a text", x = "B", y = 3, colour = "skyblue") +
theme(legend.position = c(1,0),legend.justification = c(1,0))
p1 | p2
移除图例
移除图例有好几种方式:
theme
内设置legend.position = "none"
设置 guides(fill = FALSE)
在对应的 geom
函数中,设置show.legend = FALSE
library(ggplot2)
library(tibble)
library(patchwork)
library(RColorBrewer)
colorgroup <- brewer.pal(3, "Set3")
set.seed(123)
data <- tibble(
group = rep(c("A", "B", "C"), each = 100),
value1 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2)),
value2 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2)),
value3 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2))
)
p1 <- ggplot(data, aes(x = group, y = value1, fill = group)) +
geom_violin(show.legend = FALSE) +
geom_boxplot(width = 0.1, fill = "white", color = "black", alpha = 0.5) +
scale_fill_manual(values = colorgroup) +
theme_classic() +
labs(x = "Value", y = "Group", title = "Violin Plot - Value1")
p2 <- p1 + guides(fill = FALSE)
p1 | p2
修改图例项目顺序
x
轴和y
轴的项目顺序我们使用的时scale_x_discrete和scale_y_discrete
来自定义。在修改图例顺序时,我们使用scale_fill_discrete
传入limits
参数进行自定义
library(ggplot2)
library(tibble)
library(patchwork)
library(RColorBrewer)
colorgroup <- brewer.pal(3, "Set3")
set.seed(123)
data <- tibble(
group = rep(c("A", "B", "C"), each = 100),
value1 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2)),
value2 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2)),
value3 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2))
)
p1 <- ggplot(data, aes(x = group, y = value1, fill = group)) +
geom_violin() +
geom_boxplot(width = 0.1, fill = "white", color = "black", alpha = 0.5) +
scale_fill_manual(values = colorgroup) +
theme_classic() +
labs(x = "Value", y = "Group", title = "Violin Plot - Value1")
p2 <- p1 + scale_fill_discrete(limits = c("C","A","B"))
p1 | p2
反转图例的顺序
添加guides(fill=guide_legend(reverse=TRUE))
以反转图例的顺序
修改图例的标题
较为推荐labs()
函数,使用较为简单,其中title,x,y
是默认的图形标题,x
和y
轴标题。例如在绘图中使用fill
填充了不同颜色,则对应生成的图例使用fill
来更改;size
和color
同理
library(ggplot2)
library(tibble)
library(patchwork)
library(RColorBrewer)
colorgroup <- brewer.pal(3, "Set3")
set.seed(123)
data <- tibble(
group = rep(c("A", "B", "C"), each = 100),
value1 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2)),
value2 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2)),
value3 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2))
)
p1 <- ggplot(data, aes(x = group, y = value1, fill = group)) +
geom_violin() +
geom_boxplot(width = 0.1, fill = "white", color = "black", alpha = 0.5) +
scale_fill_manual(values = colorgroup) +
theme_classic() +
labs(x = "Value", y = "Group", title = "Violin Plot - Value1")
p2 <- p1 + scale_fill_discrete(limits = c("C","A","B"))+
labs(title = "这是图形标题",fill = "这是图例标题")
p1 | p2
修改图例标题的外观
使用theme
里的legend.title
参数来调整即可
移除图例标题
library(ggplot2)
library(tibble)
library(patchwork)
library(RColorBrewer)
colorgroup <- brewer.pal(3, "Set3")
set.seed(123)
data <- tibble(
group = rep(c("A", "B", "C"), each = 100),
value1 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2)),
value2 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2)),
value3 = c(rnorm(100), rnorm(100, mean = 1.5), rnorm(100, mean = 2))
)
p1 <- ggplot(data, aes(x = group, y = value1, fill = group)) +
geom_violin() +
geom_boxplot(width = 0.1, fill = "white", color = "black", alpha = 0.5) +
scale_fill_manual(values = colorgroup) +
theme_classic() +
labs(x = "Value", y = "Group", title = "Violin Plot - Value1")
p2 <- p1 + guides(fill = guide_legend(title = NULL))
p1 | p2
修改图例标签
通过scale_fill_discrete(labels = c())
传入设置, 其他关于scale_[]
总结及使用方法详见离散型变量使用不同调色板和连续型变量使用自定义调色板
多行文本使用"斜杠n
"进行换行控制
修改图例标签的外观
通过theme
中的legend.text
进行设置
修改坐标轴刻度线的长度
在theme
中设置axis.ticks.length = unit(0.2, "cm")
修改坐标轴线条的粗细
在theme
中设置axis.line = element_line(size = 1.5)
离散型变量使用不同调色板
离散指非连续型的数据或变量。
填充色标度 | 轮廓色标度 | 描述 |
---|---|---|
scale_fill_discrete() | scale_colour_discrete() | 色轮周围均匀等距色(同 hue) |
scale_fill_hue() | scale_colour_hue() | 基于色相(hue)的离散色标度 |
scale_fill_grey() | scale_colour_grey() | 灰度色标度 |
scale_fill_brewer() | scale_colour_brewer() | 使用 ColorBrewer 调色板的颜色标度 |
scale_fill_manual() | scale_colour_manual() | 手动设置离散型变量的填充色或轮廓色 |
举例:
library(gcookbook)
library(ggplot2)
library(patchwork)
# 基础图形
h <- ggplot(heightweight,
aes(x=ageYear,
y=heightIn, colour=sex)) +
geom_point()
# Discrete
discrete_plot <- h + scale_colour_discrete() +
labs(title = "discrete_plot")
# Brewer
brewer_plot <- h + scale_colour_brewer(palette = "Set1") +
labs(title = "brewer_plot")
# Hue
hue_plot <- h + scale_colour_hue() +
labs(title = "hue_plot")
# Grey
grey_plot <- h + scale_colour_grey() +
labs(title = "grey_plot")
# Manual
manual_plot <- h + scale_colour_manual(values = c("red", "blue")) +
labs(title = "manual_plot")
# 组合拼图
(discrete_plot | brewer_plot) / (hue_plot | grey_plot | manual_plot)
比较常用的就是brewer
和manual
自定义
连续型变量使用自定义调色板
除了离散型就是连续型了。
填充色标度 | 轮廓色标度 | 描述 |
---|---|---|
scale_fill_gradient() | scale_colour_gradient() | 单一色渐变标度,适用于连续型数据 |
scale_fill_gradient2() | scale_colour_gradient2() | 双向色渐变标度,中心值具有特殊意义的连续型数据 |
scale_fill_gradientn() | scale_colour_gradientn() | 多色渐变标度,适用于指定多个颜色的连续型数据 |
举例:
#该代码来源于R可视化手册
library(gcookbook)
library(ggplot2)
library(patchwork)
library(scales)
p <- ggplot(heightweight, aes(x=ageYear, y=heightIn, color=weightLb)) + geom_point(size=3)
p1 <- p + scale_color_gradient(low = "black", high = "white")
#可以直接去除muted函数,该函数来自于scales包,目的是针对输入的颜色输出一个饱和度较低的颜色(RGB 格式)
p2 <- p + scale_color_gradient2(low = muted("red"),
mid = "white",
high = muted("blue"),
midpoint = 115)
p3 <- p + scale_color_gradientn(colors = c("red", "white", "blue", "green", "black"))
p1 / p2 / p3
主题修改中的一些技巧
文本上下标
ggplot2
包中的表达式(expression)
函数来定义带有上标和下标的标签。例如,要在标签中添加上标和下标,你可以使用以下语法:
上标: ^
下标: []
***
**: 符号用于将不同的表达式连接起来
下标
主要采用[]
进行添加
# 加载 ggplot2 包
library(ggplot2)
library(tidyverse)
# 创建数据框
data <- tibble(x = 1, y = 1)
# 绘制散点图
ggplot(data, aes(x = x, y = y)) +
geom_point(size = 5) +
labs(title = expression(O[2]*"分子式"))+
xlim(0.9,1.1)+
ylim(0.9,1.1)+
theme_classic()
上标
主要采用^
进行添加
# 加载 ggplot2 包
library(ggplot2)
library(tidyverse)
# 创建数据框
data <- tibble(x = 1, y = 1)
# 绘制散点图
ggplot(data, aes(x = x, y = y)) +
geom_point(size = 5) +
labs(title = expression(O^2*"分子式"))+
xlim(0.9,1.1)+
ylim(0.9,1.1)+
theme_classic()
上下混合标
主要采用^
进行添加
# 加载 ggplot2 包
library(ggplot2)
library(tidyverse)
# 创建数据框
data <- tibble(x = 1, y = 1)
# 绘制散点图
ggplot(data, aes(x = x, y = y)) +
geom_point(size = 5) +
labs(title = expression(y * "=" * x[1]^2 + x[2]^2(x[1]^2))) +
xlim(0.9,1.1)+
ylim(0.9,1.1)+
theme_classic()
ggplot2绘图与坐标轴所保留的间隙
ggplot2
在绘制图形时增加一些额外的空间,以便更好地展示数据。然而,我们并不完全需要这些数据。
library(ggplot2)
# 创建一个简单的数据集
my_data <- data.frame(
x_var = c(1, 2, 3, 4, 5),
y_var = c(10, 20, 30, 40, 50)
)
# 绘制散点图,并调整 x 轴的扩展
ggplot(data = my_data, aes(x = x_var, y = y_var)) +
geom_point(size = 3) +
scale_x_continuous(expand = c(0.1, 0.1))+ # 控制 x 轴的扩展
theme_classic()
在该图形中,y轴的最小值10与x轴存在空隙。这种额外的保留可以通过scale_x_continuous(expand = c(0.1, 0.1))
来删除
library(ggplot2)
# 创建一个简单的数据集
my_data <- data.frame(
x_var = c(1, 2, 3, 4, 5),
y_var = c(10, 20, 30, 40, 50)
)
# 绘制散点图,并调整 x 轴的扩展
ggplot(data = my_data, aes(x = x_var, y = y_var)) +
geom_point(size = 3) +
scale_y_continuous(expand = c(0, 0))+ # 控制 x 轴的扩展
theme_classic()
expand
参数是一个长度为2
的向量,其中第一个元素控制下限的扩展,第二个元素控制上限的扩展。这两个值通常是介于0
和1
之间的小数,表示相对于数据范围的比例。
例如,如果expand
参数设置为c(0.1,0.1)
,则表示坐标轴将在数据范围的下限和上限各扩展实际数据范围的10%
。这样做的目的是为了确保数据点不会紧密地靠近坐标轴,使得图形更易于阅读和解释。
如果将expand
参数设置为c(0,0)
,则表示不进行任何扩展,坐标轴的范围与数据的实际范围完全一致。这样可能导致数据点紧贴在坐标轴上,使得图形的可读性降低。
x轴同理
科学计数法转指数计数法
trans_format()
函数用于创建一个转换器,用于在标签上应用格式化函数。它通常与scale_-_continuous()或scale_-_date()
函数一起使用,以在绘图中更改轴的标签格式。
library(ggplot2)
library(scales)
library(tidyverse)
library(MASS)
library(patchwork)
class(Animals)
p1 <- ggplot(Animals, aes(x=body, y=brain,
label=rownames(Animals))) +
geom_text(size=3)
p2 <- p1 + scale_y_log10() +
scale_x_log10()
p3 <- p1 + scale_x_log10(breaks = 10^(1:4),
labels = trans_format("log10", math_format(10^.x)))
p4 <- p2 + scale_y_log10(labels = trans_format("log10",math_format(10^.x)))
(p1 | p2)/ (p3 | p4)
p4
trans_format("log10",math_format(100^.x))
表示将刻度值转换为以对数底为10
的对数值,并使用 math_format()
函数将转换后的对数值格式化为可读的字符串。
主要参考:
《ggplot2:数据分析与图形艺术》在线版:https://ggplot2-book.org/
《R数据可视化手册》
《R语言数据可视化之美:专业图表绘制指南(增强版)》
本文为个人学习笔记,整理过程难免有误。如有错误,欢迎指正。仅供个人学习使用,如有侵权,请联系删除