R语言可视化—饼图
今天开始进行R语言可视化的练习,主要参照的是文献《Preoperative immune landscape predisposes adverse outcome in hepatocellular carcinoma patients with liver transplantation》中的配图,尽量复现,顺便以此夯实R语言基础操作。今天复现文章中的Fig.1A,即饼图绘制。
Fig.1A文献中描述We sequenced the transcriptomes of a total of 124 samples, comprising 62 malignant tumors, 47 adjacent nontumors, and 15 normal samples, from a Korean HCC cohort (Fig. 1a and Supplementary Table 1).
首先我们从基础的极坐标绘图开始,极坐标绘图一般是先画柱形图,再转化为极坐标,如下图:
代码语言:r复制library(ggplot2)
# 创建示例数据
data1 <- data.frame(
category = c("A", "B", "C", "D"),
value = c(2, 3, 5, 4)
)
# 基本的柱状图
p <- ggplot(data1, aes(x = category, y = value))
geom_bar(stat = "identity", fill = "skyblue")
theme_minimal()
# 转换为极坐标
p coord_polar(theta = "x", start = 0)
p coord_polar(theta = "y", start = 0)
注:
ggplot(data, aes(x = category, y = value)):定义绘图的数据集和美学映射。
geom_bar(stat = "identity", fill = "skyblue"):绘制柱状图,并设置颜色。
coord_polar(theta = "x", start = 0):将柱状图转换为极坐标。
theta = "x"表示使用x轴进行极坐标转换,theta = "y"表示使用y轴进行极坐标转换,
start = 0 控制起始角度。
代码语言:r复制ggplot(data1, aes(x = "", y = value, fill = category))
geom_bar(width = 1, stat = "identity")
coord_polar(theta = "y")
将x值设为空,并且fill = category后,即可绘制常规的饼图。
接下来再对这张图进行修饰即可,观察Fig.1A,知道应该做如隐藏x,y轴、移除多余的图形元素、将value值标注在对应的色块中并且居中排列、将图例放在图的下方按照两列排列并隐藏图例名称、图例外有黑边包边。
代码语言:r复制ggplot(data1, aes(x = "", y = value, fill = category))
geom_bar(width = 1, stat = "identity")
coord_polar(theta = "y")
labs(x = NULL, y = NULL, fill = "Category")
theme_void() # 移除多余的图形元素
geom_text(aes(label = value),
position = position_stack(vjust = 0.5), color = "black")
theme(legend.position = "bottom",
legend.title = element_blank()) # 隐藏图例标题
guides(fill = guide_legend(ncol = 2,
override.aes = list(col = "black", size = 2)))
注:
position_stack(vjust = 0.5)用于控制标签在堆积条形图(或饼图)的堆叠位置中的显示方式。具体来说:
- position_stack:这是一个位置调整函数,用于在堆叠的条形图或饼图中调整元素的位置。对于堆叠的条形图,它将标签按照条形的高度依次堆叠。
- vjust = 0.5:
vjust
是垂直对齐参数,取值范围是0到1:- vjust = 0 表示标签对齐在每个堆叠部分的底部。
- vjust = 1 表示标签对齐在每个堆叠部分的顶部。
- vjust = 0.5 表示标签对齐在每个堆叠部分的中间。
在饼图中,position_stack(vjust = 0.5)用于将标签(如百分比)放置在每个饼图扇形区域的中间位置,从而使得标签更清晰地显示在每个部分的中心。vjust = 0.5确保标签垂直居中。
举例说明:
- vjust = 0:标签会贴近扇形的内圈边缘。
- vjust = 1:标签会贴近扇形的外圈边缘。
- vjust = 0.5:标签会居中,通常是最理想的显示位置。
搞懂这些基础知识就可以正式开始Fig.1A的绘制。
代码语言:r复制columnNames <- c("Normal",
"Fibrosis low (FL)",
"Fibrosis high (FH)",
"Cirrhosis (CS)",
"Dysplastic nodule low (DL)",
"Dysplastic nodule high (DH)",
"T1",
"T2",
"T3/4",
"Mixed")
Values<- c(15, 10, 10, 10, 10, 7, 16, 29, 11, 6)
Colors <- c('#bebdbd', '#bbe165', '#6e8a3c', '#546a2e',
'#f1c055', '#eb8919', '#f69693', '#f7474e', '#aa0c0b', '#570a08')
data <- data.frame(
group = columnNames,
value = Values
)
data$group <- factor(data$group,columnNames)
pie <- ggplot(data,aes(x="",y=value,fill=group))
geom_bar(width = 1,stat = "identity") #画条形图
coord_polar("y",start = 0,direction = -1) #极坐标旋转
scale_fill_manual(values = Colors)
geom_text(aes(label = value), color = "black",
position = position_stack(vjust = 0.5))
theme_void() #全白主题,无网格
theme(plot.title = element_blank(),legend.position = "bottom",legend.title = element_blank())
guides(fill = guide_legend(ncol = 2,
override.aes = list(col = "black", size = 1)))
为了方便以后复用,可以将画图的代码包装成函数
代码语言:r复制#构建一个绘图函数,绘制饼图
drPiechart <- function(columnNames,Values,Colors,outputPdf){
library(ggplot2)
library(scales)
library(RColorBrewer)
library(dplyr)
data <- data.frame(
group = columnNames,
value = Values
)
#将group列转换为因子类型,并按columnNames中的顺序排列
data$group <- factor(data$group,columnNames)
#绘制饼图
pie <- ggplot(data,aes(x="",y=value,fill=group))
geom_bar(width = 1,stat = "identity") #画条形图
coord_polar("y",start = 0,direction = -1) #极坐标旋转
scale_fill_manual(values = Colors)
geom_text(aes(label = value), color = "black",size = 4,
position = position_stack(vjust = 0.5))
theme_void() #全白主题,无网格
theme(plot.title = element_blank(),legend.position = "bottom",legend.title = element_blank())
guides(fill = guide_legend(ncol = 2,
override.aes = list(col = "black", size = 1)))
#保存图片
ggsave(outputPdf,pie,units = 'cm',height = 8,width = 16)
}
以后的数据直接调用即可
代码语言:r复制#调用函数
drPiechart(c("Normal",
"Fibrosis low (FL)",
"Fibrosis high (FH)",
"Cirrhosis (CS)",
"Dysplastic nodule low (DL)",
"Dysplastic nodule high (DH)",
"T1",
"T2",
"T3/4",
"Mixed"),
c(15, 10, 10, 10, 10, 7, 16, 29, 11, 6),
c('#bebdbd', '#bbe165', '#6e8a3c', '#546a2e',
'#f1c055', '#eb8919', '#f69693', '#f7474e', '#aa0c0b', '#570a08'),
'./results/Figure 1A.pdf')
其中如何在饼图外加分组名称暂未研究明白。