【R】画图实践

32.pheatmap 修改颜色bar

  • 王鹏提供代码
library(pheatmap)
GO_dat<-read.table("C:/Users/Administrator/Desktop/植科/DEG_CK_D/up_down/GO/M_NO/GO_dat.txt",sep="\t")

bk<-c(seq(0,0.05,by=0.01),seq(0.06,1,by=0.01))
pheatmap(GO_dat,cluster_row=F,cluster_col=F, scale="none",main="GO pheatmap",
         color = c(colorRampPalette(colors = c("yellow","white"))(length(seq(0,0.05,0.01))),colorRampPalette(colors = c("white","black"))(length(seq(0.06,1,0.01)))),
         fontsize_row = 8,legend_breaks = seq(0,1,0.1),
         breaks = bk)

31.stat / geom /position 运用

设置geom_bar()的position参数为"stack",在向条形图添加文本时,使用position=position_stack(0.5),调整文本的相对位置。

ggplot(data=Arthritis, mapping=aes(x=Improved,fill=Sex))+
  geom_bar(stat="count",width=0.5,position='stack')+
  scale_fill_manual(values=c('#999999','#E69F00'))+
  geom_text(stat='count',aes(label=..count..), color="white", size=3.5,position=position_stack(0.5))+
  theme_minimal()
image.png

30.stat 函数使用

R语言与医学统计图形-【22】ggplot2统计变换函数
R|ggplot2(四)|stat_ geom_ 和position

a <- ggplot(data.frame(x=c(-5,5)),aes(x))+
  stat_function(fun=dnorm) #传入的函数只需名字,无需括号

b <- ggplot(data.frame(x=c(-5,5)),aes(x))+
  stat_function(fun=dnorm,args = list(mean=2,sd=.5)) #传参

grid.arrange(a,b,ncol=1)
image.png

29.为什么你画的ggplot2箱线图没横线 哈哈

library(ggplot2)
ggplot(data = iris,aes(x = Species,y = Sepal.Length))+
  geom_boxplot( width = 0.3)
image.png
ggplot(data = iris,aes(x = Species,y = Sepal.Length,fill = Species))+
  stat_boxplot(geom ='errorbar', width = 0.3)+
  geom_boxplot( width = 0.3)
image.png

28.patchwork ggplot2 + 截断y轴

参考:肿么办,ggplot2不能截断纵坐标

require(ggplot2)
require(patchwork)
set.seed(13)
options(stringsAsFactors = FALSE)
name <- rep(LETTERS[1:10],3)
value <- runif(30)

data <- data.frame(name, value)
customer_theme <-  theme(axis.text.x = element_blank(),
                         axis.ticks.x = element_blank(),
                         axis.title = element_blank(),
                         panel.background = element_blank(),
                         axis.line = element_line(colour = "black"))


p1 <- ggplot(data = data, aes(x = name, y = value)) + 
  geom_bar(stat = "identity", position = "stack") +
  coord_cartesian(ylim = c(0,0.2)) #设置下面一半

p2 <- ggplot(data = data, aes(x = name, y = value)) + 
  geom_bar(stat = "identity", position = "stack") +
  coord_cartesian(ylim = c(0.24,3)) + 
  scale_y_continuous(breaks = # 按值设置breaks
                       seq(from = 0.24, to = 3, by = 0.5)) +
  labs( title = "Graph with broken y axis")

p2/p1 & customer_theme

image.png

27.ggplot2 画出ggpubr 效果

Y 叔笔记
tips: ylim(NA,8) 设置下边界。

t1 <- rnorm(10)
t2 <- rnorm(10) + 1
t3 <- rnorm(10) + 2
t4 <- rnorm(10) + 3


d <- data.frame(t1=t1, t2=t2, t3=t3, t4=t4)
d$g <- rownames(d)
require(tidyr)
dd <- pivot_longer(d, 1:4)

require(ggplot2)
ggplot(dd, aes(name, value)) + geom_boxplot() +
  geom_line(aes(group=g), color='firebrick') +
  geom_point(aes(color=name), size=3)+ylim(NA,8)
image.png

26. 堆积图按照顺序排列

  • 修改堆积的顺序
  • 注意:dat1$condition <- factor(dat1$condition,unique(dat$condition )) 一定要加上unique,因子不能重复。
    数据:
condition   up  down
m_CKD2_h_no 5090    3123
m_CKD4_h_no 4904    3420
m_CKD8_h_no 1992    1867
w_CKD2_h_no 3485    2196
w_CKD4_h_no 2979    1486
w_CKD8_h_no 2967    3058
m_CKD2_h_yes    4253    2163
m_CKD4_h_yes    4020    1518
m_CKD8_h_yes    3556    2728
w_CKD2_h_yes    2477    1524
w_CKD4_h_yes    1518    511
w_CKD8_h_yes    6245    3434
m_CKD2_c_no 3437    3060
m_CKD4_c_no 3849    6392
m_CKD8_c_no 5711    5642
w_CKD2_c_no 3854    3402
w_CKD4_c_no 2257    1498
w_CKD8_c_no 4893    5561
m_CKD2_c_yes    2126    1199
m_CKD4_c_yes    2507    1625
m_CKD8_c_yes    4679    4568
w_CKD2_c_yes    765 399
w_CKD4_c_yes    644 593
w_CKD8_c_yes    2332    2240
m_CKD2_r_no 1174    702
m_CKD4_r_no 378 312
m_CKD8_r_no 1620    1458
w_CKD2_r_no 1240    729
w_CKD4_r_no 166 139
w_CKD8_r_no 1661    572
m_CKD2_r_yes    404 253
m_CKD4_r_yes    1202    212
m_CKD8_r_yes    1557    937
w_CKD2_r_yes    307 313
w_CKD4_r_yes    223 255
w_CKD8_r_yes    716 536
library(tidyverse)
dat <- read_delim("CK_D.txt",delim = "\t",col_names = T)
# View(dat)
dat1 <- gather(dat,key=state,value = value,-condition)



dat2 <- dat %>% mutate(total=up+down) 
dat2

### Step1 :显示加和结果

ggplot(data=data.frame())+
  geom_bar(dat1,mapping = aes(x=condition ,y=value,fill=state),stat="identity")+
  scale_fill_manual(values=c("blue","red")) +
  geom_text(data = dat2,aes(x=condition,y=total,label=total),
            position=position_stack(vjust=1.05))+
  theme_classic()+
  theme(axis.text.x =element_text(angle = 90))
  
###  Step2 : 分别显示每个柱子结果
ggplot(data=dat1,mapping = aes(x=condition ,y=value,fill=state))+
  geom_bar(stat="identity")+
  # scale_fill_manual(values=c("blue","red"))+
  geom_text(aes(label=value),position=position_stack(vjust=0.5))+
  theme_classic()+
  theme(axis.text.x =element_text(angle = 90))

### 按照顺序画图
dat1 <- as.data.frame(dat1)
dat1$condition <- factor(dat1$condition,unique(dat$condition ))

ggplot(data=dat1,mapping = aes(x=condition ,y=value,fill=state))+
  geom_bar(stat="identity")+
  # scale_fill_manual(values=c("blue","red"))+
  geom_text(aes(label=value),position=position_stack(vjust=0.5))+
  theme_classic()+
  theme(axis.text.x =element_text(angle = 90))+
  xlab("sample")

image.png

如何负数barplot.但是label 为正数.

library(tidyverse)
data_sorted <- mpg %>% 
  group_by(class) %>% 
  summarise(count = n()) %>%
  mutate(class = fct_reorder(class, count))
data_sorted <- edit(data_sorted)

head(data_sorted)
#        class count
# 1    2seater     5
# 2    compact   -47
# 3    midsize    41
# 4    minivan   -11
# 5     pickup    33
# 6 subcompact   -35
# 7        suv    62

ggplot() + geom_bar(data =data_sorted, aes(x = class, y = count ,
                                           fill=ifelse(count>0,"red","blue")), 
                    stat = "identity")+
  geom_text(data =data_sorted, aes(x = class, y = count,label=abs(count)),
            position=position_stack(vjust=0.5))
image.png

25.画一维二维散点图

## 图例调节:https://www.jianshu.com/p/2ce6a9b801dd
## 修改连续变量 图例标注 https://stackoverflow.com/questions/18487369/ggplot-set-scale-color-gradientn-manually
## 修改图例标题位置 https://github.com/tidyverse/ggplot2/issues/2465

#check data
head(mtcars)

ggplot(data=mtcars, aes(x=wt, y=mpg, color=disp))+
  geom_point(size=3)+
  geom_point(aes(x=wt,y=max(mpg)+2,color=disp))+
  scale_color_gradientn(name="H3K27ac signal",
                        breaks=c(100,300,450),
                        labels=c("Low","","High"),
                        colours = c("grey","yellow","red"))+
  

  
  ## 修改图例
  theme(legend.position = "bottom",
        legend.justification=c(0.3,0.5),
        legend.key =element_rect(fill='transparent'),
        # legend.key.size = unit(1,"cm"), # 改变了行高
        legend.text = element_text(size=12))+
  ## 修改背景色,和网格
  theme(panel.grid.major =element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank())



image.png

24.画类似图形

参考: 知乎学习ggplot2
ggplot2 散点图透明度
CSDN-箱线图+散点图,学习参数
如何用ggplot2 求得平均值及其画图bar图
ggplot2-如何添加误差线

## 画出平均值
## https://stackoverflow.com/questions/11857935/plotting-the-average-values-for-each-level-in-ggplot2

df=data.frame(score=c(4,2,3,5,7,6,5,6,4,2,3,5,4,8),
              age=c(18,18,23,50,19,39,19,23,22,22,40,35,22,16))
df
ggplot(df, aes(x=factor(age), y=score)) + stat_summary(fun="mean", geom="bar")
  • 实例图形:


    2020年5月4日18:22:08
############
## time :2020年5月4日15:32:33
## author : 曹锴
## aim : 画图单边条形图
############
## 1.tab分割数据
TIME    Try Value
time-1  IgG 2
time-1  RUNX1   6
time-1  RUNX1   7
time-1  RUNX1   5
time-1  RUNX1   9
time-1  RUNX1   8
time-1  RUNX1   7
time-1  RUNX1   8
time-1  RUNX1   6
time-2  IgG 2
time-2  RUNX1   9
time-2  RUNX1   8
time-2  RUNX1   9
time-2  RUNX1   6
time-2  RUNX1   8
time-2  RUNX1   9
time-2  RUNX1   6
time-2  RUNX1   9

## 2.画图
test_df <- read.table("clipboard",header = T,check.names = F)#读取数据
str(test_df)

## 画图平均值barplot 
# 方法1:
test_df %>% group_by(TIME,Try) %>%  summarise(mean_val=mean(Value)) %>% 
  ggplot(aes(x=TIME,y=mean_val))+geom_bar(stat="identity",aes(fill=Try),position = "dodge")+
  geom_jitter(data=test_df,aes(fill=Try))
# 方法2:
test_df
ggplot(test_df, aes(x=TIME, y=Value,fill=Try)) + stat_summary(fun="mean", geom="bar",position = "dodge")+
  geom_jitter(aes(x=TIME,y=Value,colour=Try,alpha=Try),position=position_jitterdodge(jitter.width = 0.5, dodge.width = 0.9))+
  scale_color_manual(values = c("green","black"))+
  scale_alpha_manual(values = c(0,1))
2020年5月4日18:44:41
  • 添加fill 参数,画抖动图可以按照变量类型分开画,同时调节position可以修改两块之间距离.(红色及其绿色点间距)
### 修改抖动参数
ggplot(test_df, aes(x = TIME, y = Value, fill = Try)) + geom_boxplot() +
  geom_jitter(aes(color = Try),position = position_jitterdodge(jitter.width = 0.5, dodge.width = 0.9))
2020年5月4日18:45:41

23.调整横坐标轴顺序 (ggplot2/ggpubr)

  • 【ggplot2】由于R3.6 无法使用tidyverse,于是使用dplyr 进行修改横坐标因子水平. forcats包
library(dplyr)
dat <- mtcars
dat$name <- rownames(mtcars)
name <- tbl_df(dat) %>% arrange(cyl,mpg) %>% as.data.frame() %>% select(name) 
dat$name <- factor(dat$name,levels = name$name)

dat %>%  ggplot(aes(x=name,y=mpg,fill=cyl))+geom_bar(stat = "identity")+theme_bw()

  • 【ggpubr】 画图
library(ggplot2)
library(dplyr)
library(ggpubr)

dat <- mtcars
dat$name <- rownames(mtcars)
dat$cyl <-  factor(dat$cyl)

bp <- ggbarplot(dat, x = "name", y = "mpg",
          fill = "cyl",               # 通过"cyl"更改填充颜色
          color = "white",            # 将栏边框颜色设置为白色
          palette = "jco",            #  jco颜色板. 查看 ?ggpar
          sort.val = "asc",           # 按升序排序
          sort.by.groups = TRUE,      # 每个组内排序
          x.text.angle = 90           #垂直旋转x轴文本
)
bp + font("x.text", size = 8) # 修改字大小
image.png

22.画横坐标箭头


library(ggplot2) 
p1 <- ggplot(mtcars, aes(factor(cyl))) +  
  geom_bar(aes(fill = factor(gear)),               
           width = 0.6) +  
  geom_text(width = 0.6, stat = "count",            
            label = table(mtcars$cyl),            
            vjust = -0.5, size = 4, color = "red") +  
  labs(title = "标题",       
       subtitle = "副标题",       
       caption = "图一",       
       x = "cyl",       
       fill = "gear") # 更改图例名 p1






p1 + theme_bw() 
p <- p1 + theme_bw() +  
  theme(plot.title = element_text(color = "red", # 看标题发生的变化                                  
                                  size = 20, # 单位是pts,即像素                                  
                                  face = "bold", # 加粗斜体等,可选"plain", "italic", "bold", "bold.italic"                                  
                                  hjust = 0.5, # 水平方向位置,取0-1之间的数,默认0靠左,此时0.5居中                              
                                  vjust = 0.1,# 竖直方向位置(改变angle参数时字书写的方向改变,vjust是与字写的方向垂直的方向)                              
                                  angle = 90 ),# 默认正着放是0,取值0-360   
axis.text.x = element_text( # 看横坐标轴刻度标签的变化                                   
  size = rel(1.5), # rel表示是原来的多少倍                                   
  angle = 45), # 当轴刻度标签名过长时使用 

axis.line.x = element_line(color = "red", # 看横坐标轴线的变化                                   
                           size = 2,                                   
                           linetype = 6, # 0-6 各种线型,和基础的plot函数lty参数相同                                   
                           arrow = arrow(angle = 45, length = unit(0.5, "inches")), # 箭头头的长度 和角度                                  
                           lineend = "butt" ,# 线段的头是圆润的、不规则、直角  "round", "butt", "square"                                   
                           inherit.blank = F)) # 如果T就去掉了这条                                   ),        

p + theme(legend.background = element_rect(fill = "lightgray", # 更改图例背景颜色                                         
                                color = "blue", # 更改图例边框颜色,为了不和下面panel.border冲突就不在此更改                                         
                                linetype = 5, # 外框线的样式                                         
                                size = 2), # 外框线的粗细    
                                panel.border = element_blank(), # (绘图区的外框没有了)blank就是去掉 rect的        
                                axis.ticks.y = element_blank(), # (y轴的刻度没有了)去掉line的(text的也能去,但是会出warning)        
                                legend.margin = margin(40,15,10,3), # 三个数按照上右下左的顺序,指定图例的四个方向留多少空隙        
                                legend.key.size = unit(2,"cm")) # 指定长度和单位,各种单位可以用?unit查看        
  

image.png

21.视频小教程_如何画出没有教程的图?

画出三角形
ggforce 可以画出局部放大图
ggplot2 添加阴影面积

p1 <- ggplot(data1,aes(x=x, y=y)) +
  ## 画出上三角
  geom_polygon(data = data1,aes(x=x, y=y, group=group,fill=exp),color="black") +
  ## 上三角用表达值来配色
  scale_fill_gradientn(colours = c(nake,red)) +
  ## 神技能,清空aes
  new_scale_fill() +
  ## 画出下三角
  geom_polygon(data = data2,aes(x=x, y=y, group=group,fill=del),color="black") +
  ## 下三角用表达值来配色
  scale_fill_gradientn(colours = c(nake,blue))+
  ## 调整x轴
  scale_x_continuous(limits = c(1, NA), expand = c(0,0),breaks = c(1:33)+0.5,labels=xlabels) +
  ## 调整y轴
  scale_y_continuous(limits = c(1, NA), expand = c(0,0),breaks = c(1:20)+0.5,labels=ylabels) +
  ## 定主题
  theme(axis.title.x=element_blank(),
        axis.title.y=element_blank(),
        axis.text.x = element_text(angle = 90, hjust = 1))+
  ## legend置于底部
  theme(legend.position="bottom")+
  theme(plot.margin = margin(0.5,0.01,0.5,0.01, "cm"))
p1



image.png
data <- data.frame(
  x = rep(c(1,1,2,2),3),
  y = c(1,3,3,1,3,14,14,3,14,21,21,14),
  type= rep(c("Eraser","Reader","Writer"),each=4)
)

p2 <- ggplot(data,aes(x=x, y=y)) +
  geom_polygon(aes(x=x, y=y, group=type,fill=type),color="black",alpha = 0.5)+
  scale_fill_manual(values = c(red,nake,blue))+
  ## 打标签
  geom_text(data=data.frame(x=1.5,y=c(2,8.5,17.5)),
            aes(label=c("Eraser","Reader","Writer")),
            angle = 90,
            size=4)+
  scale_x_continuous(limits = c(1, NA), expand = c(0,0))+
  scale_y_continuous(limits = c(1, NA), expand = c(0,0)) +
  theme(axis.title=element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank())+
  theme(legend.position="none")+
  theme(plot.margin = margin(0.5,0.1,0.5,0.01, "cm"))
p2

image.png

拼图

library(patchwork)
p1 + p2 +
  plot_layout(widths = c(33, 1))
## 
library(cowplot)
plot_grid(p1,p2,align = "h", axis = "tb",nrow = 1, rel_widths = c(33, 1))
image.png

20.重要的是图表思维,而不是工具

image.png

19.画图突变前后表达量变化

# 减小图例大小:https://www.cnblogs.com/jessepeng/p/11454520.html
# 链接: https://pan.baidu.com/s/1jqKnmDZrJloj8Pu0ThqBdg 提取码: ysqs 

d <- read_delim(file.choose(),col_names = T,delim="\t")
d2 <- tidyr::gather(d,key="group",value="expression",c(cancer_exp ,normal_exp)) %>% as.data.frame()

library(ggplot2)
library(ggrepel)
ggplot(data.frame())+
  geom_point(data=d2,
             aes(x=distance, y= expression,color=group))+
  geom_text_repel(data=subset(d2,group=="cancer_exp"),
                  aes(x=distance, y= expression,label=symbol))+
  geom_segment(data=d,aes(x = distance, y = normal_exp, 
                   xend = distance, yend = cancer_exp
                   ),colour = "grey",show.legend = F,linetype=2, # 不显示颜色图例 show.legend
                   arrow = arrow(length = unit(0.1, "inches")))+

  scale_color_manual(values = c("red","black"),
                     name="Group")+
  scale_x_continuous(limits = c(-1000000,1000000),
                     breaks = c(-1000000, -500000, 0 ,500000 , 1000000),
                     label = c("-1Mb", "-0.5Mb", "0" ,"0.5Mb","1Mb"))+
  xlab("Distance to SNP position")+
  ylab("Gene expression(TPM)")+
  ggtitle("Expression changes before and after mutation",subtitle = "TCGA_PRAD_AR_motif rownum:6 ")+
  theme_classic()+
  theme(legend.title = element_text(size = 10), 
        legend.text = element_text(size = 8))
  
               
  
图形

18.碱基布局图

目标图:


2020年2月23日21:32:45

画图的最终图:


2020年2月23日22:09:42

tips:
缺少对右上标实现,如C* , 可能需要ggtext 来支持html 语法
学习·case_when· 使用方法。ggplot2 绘制带星号和 Pvalue 值的相关系数热图


实现过程:

# 用"="不是"<"
test <- data.frame("rownum" = rep(c(1,2,3),each=29),
                   "colnum" = rep(1:29,3),
                   "base" = sample(c("A","C","G","T"),29*3,replace = T),
                   check.names = F)
                  
edit(test)
#View(test)



tag<- c(1,2,3,4)
names(tag) <- c("A","C","G","T")

test$colorvalue <- unlist(sapply(test$base,function(x){tag[[x]]}) )


test$colorvalue <- factor(test$colorvalue)

##### 添加*标签
#需要添加"*" 行数
# 如 5,8,6
test$base <- as.vector(test$base)
Label <- rep("",nrow(test))
Label[c(5,6,7)] <- rep("*",3)


test$base <- paste0(test$base,Label)



## 画图
P1 <- ggplot(data.frame())+
  geom_tile(data=test,aes(colnum,rownum,fill=colorvalue),color="black")+
  geom_text(data=test,aes(colnum,rownum,label=base))+
  theme_void()+
  theme(legend.position = "NA")+
  scale_fill_manual(values = c("#659c41","#90f0d5","#f6cfa6","#db7fae"))

P2 <- ggplot(data.frame())+
  geom_text(aes(x=rep(1,3),y=1:3,
                label=c("H3","H2","H1")),
                size=5)+
  ylim(0.5,3.5)+
  xlim(0.5,1.5)+
  theme_void()

library(cowplot)
combined_plot <- insert_yaxis_grob(P1, P2, position = "left",width = grid::unit(0.5, "in"))
plot_grid(combined_plot)
                                   
                                   

17.画GO富集图

果子学生信
教程
reorder

##############################
library(ggplot2)    #载入绘图的包
 # data <- read.csv( file = "https://raw.githubusercontent.com/caokai001/Bioinformatic_code/master/R/GO%E5%88%86%E6%9E%90/GO1_H3K4me3.txt",header = T,sep = " ")

data <- read.csv( file.choose(),header = T,sep = " ")
filter_data <- data[,c(2,3,8)]
head(filter_data)
# select top 15
filter_data <- tbl_df(filter_data) %>% mutate(pvalue=-log10(pvalue)) %>%
arrange(term_type,desc(pvalue)) %>% group_by(term_type) %>% top_n(15,pvalue) 


# 画图
ggplot(filter_data,aes(x=factor(Term,levels =filter_data$Term) ,y=pvalue)) +
  geom_bar(aes(fill=term_type),stat = "identity",width=0.5,position = position_dodge(20))+
  coord_flip()+
  theme_bw()+
  xlab("GO term")  +
  ylab("-log10(Pvalue)")+
  guides(fill=guide_legend(title="GO_categary"))+
  theme(axis.text.x =element_text(angle=90),panel.background=element_rect(fill='transparent',color ="black"))



潜哥代码

library(ggplot2)
up$Term <- factor(x$name, levels = x$name[order(x$val)])
a <- ggplot(up, aes(reorder(Term, -PValue), -log10(PValue))) + 
  geom_histogram(stat = "identity", position = "stack", boundary = 0) + 
  coord_flip() + theme(legend.position="none") + scale_x_discrete(position="right")


b <- ggplot(down, aes(reorder(Term, PValue), -log10(PValue), fill = "green")) + 
  geom_histogram(stat = "identity") + 
  coord_flip() + theme(legend.position="none") + scale_x_discrete(position="left")

library(cowplot)
plot_grid(a, b, ncol = 2)

setwd("E://desktop/sept/Y_picture/")
up <- read.table("Up.GO.bp.txt", sep = "\t", header = T)
down <- read.table("Down.GO.bp.txt", sep = "\t", header = T)
library(tidyverse)

new_up <- up %>% mutate(logPvalue = -log(up$PValue), group = factor("up"))
new_down <- down %>% mutate(logPvalue = log(up$PValue),group = factor("down"))
data <- rbind(new_up[,c(1, 3, 4)], new_down[, c(1, 3, 4)])

library(ggthemes)
ggplot(data, aes(reorder(Term, logPvalue), logPvalue, fill = group)) + 
  geom_histogram(stat = "identity", position = "stack", boundary = 0) + 
  scale_y_continuous(labels = c(10, 5, 0, 5, 10)) +
  coord_flip() + theme(legend.position="none") + scale_x_discrete(position="right") +
  # Tufte theme from ggfortify
  scale_fill_brewer(palette = "Dark2")   +# Color palette
  xlab(label = "") + ylab(label = "-log(Pvalue)") +
  theme_bw() +
  theme(axis.text.y = element_text(size=20),axis.text.x = element_text(size=20),
        legend.title = element_text(size=15),legend.text = element_text(size=15),
        axis.title.x = element_text(size=20),
        legend.key.height=unit(1.2,'cm'),
        legend.position = c(0.8, 0.2),
        panel.grid=element_blank()) +
  guides(fill = guide_legend(title = NULL))

16.小练习

  • geom_label :https://www.jianshu.com/p/cde77a937096library(tidyverse)
  • arrow(length=unit(0.3,"cm")) : https://stackoverflow.com/questions/3421331/example-needed-using-arrow-with-ggplot2
## geom_label 参考:https://www.jianshu.com/p/cde77a937096library(tidyverse)
value=round(graph_n(),4)  ## 保留两位数字
ggplot()+  
geom_point(aes(x=seq(0.5,1,0.05)[1:10],y=value) )+
ylim(0,1)+labs(x="cutoff",y="predict_score",caption="CENTIPEDE:层次贝叶斯:AR",title="the realtion between cutoff and predict score ")+
geom_text(aes(x=seq(0.5,1,0.05)[1:10],y=value+0.05),label=value,check_overlap = TRUE)+  ## 防止字符串重叠
geom_segment(aes(x=seq(0.5,1,0.05)[1:10],xend=seq(0.5,1,0.05)[1:10],y=value+0.04,yend=value),color = "purple",arrow =arrow(length=unit(0.3,"cm")))   ## 修改箭头侧翼长度

image.png

15.多边形填充

https://ggplot2.tidyverse.org/reference/geom_polygon.html

image.png
### 
data=data.frame(
  x=c(0,0,1,1,0,0,1,2),
  y=c(2,0,0,2,3,2,2,3),
  group=rep(c("g1","g2"),each=4)
  
)
ggplot(data = data,aes(x=x,y=y))+geom_polygon(aes(fill=group))+
  scale_fill_manual(values = c("grey80","grey80"))+
  guides(fill=FALSE)
image.png

14.ggplot2 高速公路

心得体会:
  • 1.row_number() 意义上和rank一样,就是排序。假设分组后进行rank,可以得到那个排第一。然后就可以filter(rank==1) 提取结果。
image.png
  • 2.pivot_longercols 列进行变换。键为city_fuel,值为city_mpg
pivot_longer(cols = c("city08", "cityA08"), names_to = "city_fuel", values_to = "city_mpg")
  • 3 fct_reorder(make, highway_mpg_median) 对于make排序。先按照highway_mpg_median 排序,再提取make列。

  • 4 如何画虚线 geom_segment( linetype=2);
    grey100 :白色 ;grey0 :黑色
    提取分组后计算的结果,ungroup() 取消分组,top_n() 提取前多少个值。

公路




13. 火山图 cuffdiff

library(ggplot2)
library(tidyverse)

data=read.table(file = "gene_exp.diff",header = T,row.names = 2)
head(data)
data<-tbl_df(data)%>%filter(data$log2.fold_change.!="Inf" & data$log2.fold_change.!="-Inf" )

threshold <- as.factor(ifelse(data$p_value < 0.05 &
                                abs(data$log2.fold_change.) >= 1 ,
                              ifelse(data$log2.fold_change. >= 1 ,'UP','DOWN'),'NOT'))
ggplot(data)+geom_point(aes(x=log2.fold_change.,y=-log10(p_value),color=threshold))+ggtitle("Diff_exp")+
  scale_color_manual(values = c("blue","black","red"))+theme_classic()+coord_fixed(ratio = 4)+
  labs(x="Log2FC",y="-Log10P")+
  geom_hline(yintercept=-log10(0.05),linetype=4)+
  geom_vline(xintercept=c(-0.5,0.5),linetype=4)+
  guides(colour = FALSE)+ # 去掉图例 
  theme(axis.text=element_text(size=15),axis.title=element_text(size=15))+xlim(-8,8)

View(data)

image.png

12.scale_color_discrete()

  • scale_fill_identity(guide = "legend") : 将填充颜色替换成随机变量,legend 会消失。需要添加guide 添加图例
df0 <- data.frame(
  x = rep(1:4,each=2),
  y = rep(1:4, 2),
  colour = c("red", "green", "blue", "yellow")
)

ggplot(df0,aes(x,y)) +geom_col(aes(fill=colour)) + 
  scale_fill_identity(guide = "legend") # 把图例加回来

  • coord_fixed(ratio=0.4) ##设置xy轴的比例,不随着窗口放大缩小,改变比例。
  • scale_x_discrete(limits=c("1", "2","3"),position = "bottom") : 连续变量改成离散型,limit 改标签
library(ggplot2)
pathway <- read.table(file.choose(),header = T,sep="\t")


pathway$Class <- as.factor(pathway$Class)

ggplot(pathway,aes(SampleGroup,Description,size=pvalue)) +
  geom_point() +
  scale_size_continuous(limits=c(0,5), breaks = c(1,2,3,4,5)) +
  geom_point(aes(color=factor(Class)))+
  scale_colour_discrete() +
  scale_x_discrete(limits=c("1", "2","3"),position = "bottom") +
  coord_fixed(ratio=0.4) ##设置xy轴的比例

image.png
  • 图例名称修改:labs()
 labs(color=expression(-log[10](Qvalue)),size="Gene number",  
       x="Gene number",y="Pathway name",title="Top20 of pathway enrichment") 

11.图片中嵌入图片

  • https://www.jianshu.com/p/5bd2a8d0428c
  • 图形矩阵拼接 包括cowplot
library(ggplot2)
df<-data.frame(x=LETTERS[1:10],y=10:1)
df
p1<-ggplot()+
  geom_bar(data=df,aes(x=x,y=y,fill=x),
           stat="identity")+theme_bw()+
  theme(legend.position = "none")+
  theme(panel.border = element_blank(),
        panel.grid = element_blank())

p2<-ggplot()+
  geom_bar(data=df[1:5,],aes(x="",y=y,fill=x),
           stat="identity")+theme_bw()+
  coord_polar("y",start=0)+
  theme_bw()+
  theme(axis.title = element_blank(),
        axis.text = element_blank(),
        legend.position = "none",
        panel.border = element_blank(),
        panel.grid = element_blank())+
  scale_fill_brewer(palette="Set1")
g<-ggplotGrob(p2)
p1+annotation_custom(g,xmin=5,xmax=12,ymin=5,ymax=10)
image.png

10 .潜哥绘图

case_when-dplyr/
lead_lag 求增长率方便
ggthemes 离散配图颜色
vars() 使用

image.png

保持x_y_长度一样coord_equal() :geom_curve 用法如下图;

  • curvature 画曲率【0-1】,越大幅度越大
  • angle 【0-180】 ,曲线偏移程度,90度正常的曲线。
ggplot() + 
  geom_curve(data = df %>% filter(x_gt_y_equal_xy_sign),
             aes(x = x, y = y, xend = x.to, yend = y.to, color = x_gt_y_equal_xy_sign),
             curvature = 0.5, angle = 90,
             arrow = arrow(length = unit(0.25,"cm")))+coord_equal()
image.png

if_else() 与case_when() 多条件实例

x=1:50
case_when(x<5 ~"litter",x>40 ~"bigger",TRUE~"other")
if_else(x<5,"litter",if_else(x>40,"bigger","other"))


连续变量调节图例-scale_color_gradientn(rescale)

#####################
df <- data_frame(x.to = c( 6,5),
                 y.to = 0.1,
                 x = c(1,4),
                 y = 0.1,
                 ratio=c(0.2,0.4))
rect <- data_frame(x.to =c(2:6) ,
                 y.to =0.2 ,
                 x = c(1:5),
                 y = 0)
library(RColorBrewer)
library(scales)
library(ggplot2)
library(RColorBrewer)
library(ggthemes)
ggplot(rect)+
  geom_rect(aes(xmin = x, ymin = y, xmax = x.to, ymax = y.to,fill=x))+
  scale_y_continuous(expand = c(0,2))+
  geom_curve(data = df,aes(x = x, y = y, xend = x.to, yend = y.to,col=ratio))+
  scale_fill_gradientn(limits = c(0, 8), # 指定颜色填充的范围
                       colors = brewer.pal(n = 5, name = "RdYlGn"), # 选取颜色,对应颜色注释见 `display.brewer.all()`,
                       values = rescale(c(0,2,4,6,8)),
                      labels=c("ok","great","1","2","6")
  )

image.png
  • 不等长df 合并
merge(df, rect, by = "row.names", all = TRUE)
  • 类似loop效果
library(RColorBrewer)
library(scales)
library(ggplot2)
library(RColorBrewer)
library(ggthemes)
#data=merge(rect,df,by = "row.names",all=TRUE)
data=read_csv("https://raw.githubusercontent.com/caokai001/caokai001.github.io/master/loop.test.csv")
ggplot(data)+
  geom_rect(aes(xmin = x, ymin = y, xmax = x.to, ymax = y.to,fill=x))+
  scale_y_continuous(expand = c(0,2))+
  geom_curve(aes(x = X, y = Y, xend = X.to, yend = Y.to,col=ratio))+
  scale_fill_gradientn(limits = c(0, 8), # 指定颜色填充的范围
                       colors = brewer.pal(n = 5, name = "RdYlGn"), # 选取颜色,对应颜色注释见 `display.brewer.all()`,
                       values = rescale(c(0,2,4,6,8)),
                      labels=c("ok","great","1","2","6")
  )+facet_grid(.~data$chr)

image.png

9.ggplot2 画热图

  • 使用ggplot2画简单热图
#使用ggplot2画简单热图
library(tidyverse)
library(ggplot2)
data <- as.data.frame(matrix(rnorm(9*10),9,10))

rownames(data) <- paste("Gene", 1:9, sep="_")
colnames(data) <- paste("sample", 1:10, sep="_")

data$ID <- rownames(data)
# gather 键值
data_m <- gather(data, attribute,value,-ID)
View(data_m)

# 画图
p <- ggplot(data_m, aes(y=attribute,x=ID)) + 
  xlab("samples") +  theme_classic() + theme(axis.ticks = element_blank(),
                                             axis.line = element_blank()) + 
  theme(panel.grid.major = element_blank()) + 
  theme(legend.key=element_blank())  +
  theme(axis.text.x=element_text(angle=45,hjust=1, vjust=1)) + 
  theme(legend.position="top") +  
  geom_tile(aes(fill=value)) + 
  scale_fill_gradient2("Expression",
                       low = "green", 
                       high = "red",
                       mid = "black")
p
image.png

8.

1.画图顺序

  • 如何调整画图x顺序 [http://yangl.net/2017/07/05/ggplot-x-sort/]-(http://yangl.net/2017/07/05/ggplot-x-sort/)
    指定factor(data$exp,levels=c(具体顺序))
  • GO 富集分析 画图顺序
    label 展示顺序
GO_term_order=factor(as.integer(rownames(data)),labels=data$GO_term)

2.legend


library(ggplot2)
bp <- ggplot(data=PlantGrowth, aes(x=group, y=weight, fill=group)) + geom_boxplot()
bp
#无图例
bp + guides(fill=FALSE)
#无图例title
bp+guides(fill=guide_legend(title = NULL))
# 修改title
bp+guides(fill=guide_legend(title = "hel"))
# 修改图例内容
bp+scale_fill_manual(name="",values = c("black","blue","grey"),label=c("1","2","3"))
# 颠倒图例方法
bp+guides(fill=guide_legend(reverse = TRUE))


##自定义

bp + scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"), 
                       name="Experimental\nCondition",
                       breaks=c("ctrl", "trt1", "trt2"),
                       labels=c("Control", "Treatment 1", "Treatment 2"))
# 修改legend 位置

bp + theme(legend.justification=c(0,0), # 这个参数设置很关键
           legend.position=c(0,0))

7.潜哥画图

aplot图

总结:
    1. 【p1】labs(x = "Methylation difference (beta-value)", y = bquote(~-log[10]~(italic("P-value")))) + #参考高级火山图
      实现text倾斜
  • 2.【p2】 inherit.aes = FALSE在一个ggplot绘图中,ggplot(data=xxx,aes(x,y))geom_point(data=xxx,aes(x,y))调用不同数据时候,需要添加上,不同的映射。
    1. 拼图cowplotinsert_xaxis_grob,将多个图柔和在一起。
    1. guides(colour = guide_legend(override.aes = list(size=5))) # 修改 legeng size 大小
## 构造数据
Percent=data.frame(feature=c("1stExon","3'UTR", "5'UTR" ," Body"  ,"IGR",  "TSS1500", "TSS200" ),
             n=c(143, 194 ,318, 1659, 953 , 354 ,270),
             percent=c("3.7%" , "5.0%" ,"8.2%","42.6%","24.5%","9.1%","6.9%"))

Percent$A<-1:7

test = data.frame(x = c(1, 2, 3, 4, 5, 6, 7),
                  y = rep(-200, 7),
                  type = LETTERS[1:7])
## 画图
#hvjust 左底部适应,字体方向不随着坐标轴旋转而改变。hvjust 按照新坐标系进行调整。


p2<-ggplot(Percent, aes(A, n))+geom_bar(stat = "identity", fill = "#d9d9d9",color = "black")+
  geom_point(data=test,aes(x,y,color=type),size=4)+
  geom_text(aes(label = Percent$percent,
                hjust = ifelse(n > 1000, 2, -0.2)),
            #nudge_y = -120, 
            vjust = 0,
            fontface = "bold", size = 3)+
  geom_text(data=Percent,aes(A,-800,label=feature,hjust=0.2),fontface = "bold",size=3)+
  geom_rect(data=rect_data,inherit.aes = FALSE,aes(xmin = xstart, xmax = xend,
                               ymin = ystart, ymax = yend),color="black",fill=NA)+coord_flip()+
  theme_bw()+
  scale_x_continuous(expand = c(0,0))+
  scale_y_continuous(expand = c(0,0))+
  theme(panel.background = element_blank(),
        panel.border = element_blank(),
        axis.title = element_blank(),
        axis.ticks = element_blank(),
        axis.text = element_blank(),
        legend.position = "none",
        panel.grid = element_blank())

### p3
df1 <- data.frame(xmin = c(-350, 500, 1928, 4591),
                  xmax = c(500, 1928, 4591, 6500),
                  ymin = 00,
                  ymax = 100,
                  class  = c("A","B","C","A"),
                  text = c("31.56%",  "", "", "68.44%"))
p3<-ggplot(df1,aes(xmin= xmin, xmax = xmax, 
               ymin= ymin , ymax= ymax, 
               fill = class, label = text ))+
  geom_rect(color="black")+
  scale_fill_manual(values = c("#FFFFFF", "#4D4D4D", "#BEBEBE", "#FFFFFF"))+
  theme_bw()+
  scale_x_continuous(expand = c(0,0))+
  scale_y_continuous(expand = c(0,0))+
  geom_text(aes(label = df1$text, x = xmin + 400, y = ymax - 50))+
  theme(panel.background = element_rect(fill=NA),
        panel.border = element_rect(color = NA, fill = NA, size = 2),
        legend.position = "none",
        axis.line = element_blank(),
        axis.text=element_blank(),
        axis.ticks = element_blank(),
        axis.title = element_blank())



###p1
library(ggrepel)
library(cowplot)
p <- ggplot(mtcars, aes(wt, mpg, label = rownames(mtcars)))
p1<-p + geom_label_repel()

# 在纵向添加图形,top, 高度 0.4 in
combined_plot <- insert_xaxis_grob(p1, p3, 
                                   position = "top",
                                   height = grid::unit(0.4, "in") # 调节高度比
) 
# 横向添加图形,right,宽度2.5 in
combined_plot <- insert_yaxis_grob(combined_plot, p2, 
                                   position = "right",
                                   width = grid::unit(2.5, "in")
)

title_theme <- ggdraw() +
  draw_label("Chromosome", x = 0.4, hjust = 0, size = 18, fontface = "bold")

subtitle_theme_1 <- ggdraw() +
  draw_label("Hyermethylated DMPs          Hypomethylated DMPs",
             x = 0.1, y = 0.32,
             hjust = 0, vjust = 1.01, size = 10,  fontface = "bold")
plot_grid(title_theme, subtitle_theme_1, combined_plot, ncol = 1, rel_heights = c(0.1, 0.1, 1))
image.png

6 潜哥画图指导
一步步画
https://github.com/tidyverse/forcats

reorder(name, -age_mort) ,name 按照age_mort降序排列

5 ggplot2 扇形图
主要rev(P) ,添加文字信息。

###
library(RColorBrewer)
library(ggThemeAssist)

blank_theme <- theme_minimal()+
  theme(
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    panel.border = element_blank(),
    panel.grid=element_blank(),
    axis.ticks = element_blank(),
    plot.title=element_text(size=14, face="bold")
  )

T=data.frame(P=c(1,3,2),B=c("A","B","C"))




test<-ggplot(data=T,aes(x=1,y=rev(P)))+geom_bar(aes(fill=B),position = "stack",stat = "identity",width = 1)+
  coord_polar("y",start = 0)+
  # scale_fill_manual(values = c("#e5f5f9","#99d8c9","#2ca25f"))
  scale_fill_brewer(palette = "Dark2")+
  blank_theme  +# 去掉背景
  theme(axis.text = element_blank(),legend.title = element_blank())+
  geom_text(aes(y = P/3 + c(0, cumsum(P)[-length(P)]), 
              label = percent(P/6)), size=3)


4.GC 统计画图
qian

library("seqinr")
library(Biostrings)
library(ggplot2)
#string=readDNAStringSet("test1.fa", "fasta")
string=read.fasta("test1.fa")
stringseq <- string[[1]]
stringseq[1:100]
GC(stringseq[1:100])

slidingwindowplot <- function(windowsize,inputseq){
  starts=seq(1, length(inputseq)-windowsize, by = windowsize)
  n=length(starts)
  chunkGCs=numeric(n)
  for (i in 1:n){
    chunk<-inputseq[starts[i]:(starts[i]+windowsize-1)]
    chunkGC <- GC(chunk)
    print(chunkGC)
    chunkGCs[i]<-chunkGC
  }
  ggplot()+geom_line(aes(starts,chunkGCs))
}
slidingwindowplot(5, stringseq)

image.png

统计cpg 含量

image.png

3.table()函数 | 多用& |signif

trainSet2 <-data.frame(
  size=c("大","小","大","大","小","小"),
  weight=c("轻","重","轻","轻","重","轻"),
  color=c("红","红","红","绿","红","绿"),
  taste=c("good","good","bad","bad","bad","good")
)
table(trainSet2$size=="大" & trainSet2$color=="红")
# 有效数字
> signif(0.00512,2)
[1] 0.0051

2 position

简单画bar图,但是用颜色填充就好,position 只是堆积方式不一样
ggplot(Salaries)+geom_bar(aes(x=rank,fill=sex),position = "dodge")
image.png

1. cowplot 拼接;ggscatter;lapply

点评:

1.grid.arange(grobs ) 组合
2.ggscatter 可以画出回归直线及其p,r .
3.lapply 如果对matrix 使用,遍历所有元素。
library(pheatmap)
expr=rnorm(1000)
#dim(expr)=c(100,10)
expr=matrix(expr,ncol = 10)
colnames(expr)=LETTERS[1:10]
pheatmap::pheatmap(expr)
ITH_genes=LETTERS[1:10]
math=rnorm(10)

##[apply 介绍]http://blog.fens.me/r-apply/
#lapply 将对matrix每个元素 进行操作
x <- cbind(x1=3, x2=c(2:1,4:5))
sapply(data.frame(x), mean)
lapply(as.vector(x), function(i){print(i)})

##使用cowplot拼图

box <- lapply(ITH_genes,function(i) {
  dat=data.frame(gene=as.numeric(expr[,i]),
                 math=math) 
  head(dat)
  ## 画boxplot 
  library(ggpubr)
  
  ggscatter(dat, x = "gene", y = "math",
            color = 'black', shape = 21, size = 0.5, # Points color, shape and size
            add = "reg.line",  # Add regressin line
            add.params = list(color = "blue", fill = "lightgray"), # Customize reg. line
            conf.int = TRUE, # Add confidence interval
            cor.coef = TRUE, # Add correlation coefficient. see ?stat_cor
            xlab = i,
            cor.coeff.args = list(method = "pearson",  label.sep = "\n")
  )
  
  
})
# library(cowplot)
# plot_grid(plotlist=box, ncol=5 )
library("gridExtra")
grid.arrange(grobs=box,ncol=5)

批量画图组合
dat
ggplot(data=dat,aes(x = gene, y = math))+
  geom_point(size=0.5,shape=21,color="black")+
  geom_smooth(method = "lm",alpha=0.2)+
  annotate("text",x=-2,y=1,col="purple",label=as.character(cor(dat["gene"],dat["math"])))+
  scale_x_continuous(limits = c(-2,3),breaks=c(-2,0,1),labels=c("yes","ok","Hi"))
image.png

你可能感兴趣的:(【R】画图实践)