目录
数据整理
初级绘图
升级细节
绘制点图添加曲线和特征筛选-CSDN博客
肿瘤免疫反应瀑布图(源于The Miller Lab)_肿瘤瀑布图-CSDN博客
数据整理
rm(list = ls())
library(ggplot2)
library(dplyr)
library(ggrepel)##防止标签重叠
#模拟数据
data <- mtcars##数据查看
data1 <- data[order(data$mpg),]#进行排序
data1$ID <- paste0("",1:nrow(data1))##添加排序的ID
data2 <- data1[,c(1,12)]#[1] "mpg" "ID" #注意需要转换为数值型
data3 <- apply(data2,2,as.numeric) #转化为数值型
rownames(data3) <- rownames(data2)
data3 <- as.data.frame(data3)
data3$name <- rownames(data3)#进行需要显示标签的单独列整理
data3[1:5,]
data4 <- data3[c(6:27),]
data4$name <- ""
data5 <- data3[c(1:5,28:32),]
data6 <- rbind(data4,data5)
data6 <- data6[order(data6$ID),]##将6-27行的name 变为空值
> head(data6) mpg ID name grou Cadillac Fleetwood 10.4 1 Cadillac Fleetwood 1 Lincoln Continental 10.4 2 Lincoln Continental 1 Camaro Z28 13.3 3 Camaro Z28 1 Duster 360 14.3 4 Duster 360 1 Chrysler Imperial 14.7 5 Chrysler Imperial 1 Maserati Bora 15.0 6
初级绘图
##设置颜色分组##
data6$grou <- c(rep(c("1","2","3"),c(5,22,5)))##设置分组
data6$grou <- as.factor(data6$grou)
P6 <- ggplot(data6,aes(x = ID, y = mpg,fill = grou)) +#添加分组设置
geom_point()+
#geom_text_repel(aes(label = name), size = 3)
geom_label_repel(aes(label = name), size = 3)+#生成带边框的数据
geom_smooth(method = "loess")+##生成拟合曲线"lm"
theme_bw() + theme(panel.grid=element_blank())##去除背景和网格线
P6
dev.off()
####################################################
注意:ggrepel::geom_label_repel()函数有个参数是max.overlaps,作用是把高度重叠的标签去掉, 默认10,
自己改大。
geom_label_repel(aes(label = name), size = 3,
max.overlaps = getOption("ggrepel.max.overlaps", default = 20))#生成带边框的数据
升级细节
#添加相关线条及坐标系的设置
P1 <- ggplot(data6,aes(x = ID, y = mpg,fill = grou)) +#添加分组设置
geom_point()+
#geom_text_repel(aes(label = name), size = 3)+#没有边框
geom_label_repel(aes(label = name), size = 3)+#生成带边框的数据
geom_smooth(method = "loess")+##生成拟合曲线"lm"
geom_hline(yintercept=c(0,40),linetype=2,size=0.25)+ #设置y轴坐标
geom_hline(yintercept = c(20),linetype=1,size=0.5)+ #设置y轴的0刻度线(也就是Y轴的横线)
geom_vline(xintercept = c(16),linetype=2,size=0.5)+ #1 表示实线,2为虚线
theme_bw() + theme(panel.grid=element_blank())##去除背景和网格线
P1
dev.off()