查看原文
其他

R绘图模板——气泡图绘制!

王志山 科研后花园 2023-09-08
 

点击上方

“科研后花园”

关注我们

代码如下:

1、设置工作环境及加载R包:

rm(list=ls())#clear Global Environmentsetwd('D:\\气泡图')#设置工作路径#加载包library(ggplot2) # Create Elegant Data Visualisations Using the Grammar of Graphicslibrary(ggprism) # A 'ggplot2' Extension Inspired by 'GraphPad Prism'library(RColorBrewer) # ColorBrewer Paletteslibrary(grid) # The Grid Graphics Packagelibrary(scales) # Scale Functions for Visualization

2、加载数据:

df <- read.table(file="data.txt",sep="\t",header=T,check.names=FALSE)

3、基本绘图:

p1<-ggplot(df,aes(A,B,fill=C))+ geom_point(aes(size=D,color=C))p1

4、绘图:

ggplot(df,aes(A,B,color=C,size=D,fill=C))+#色 geom_point(color="black",#气泡边框色 shape=21,alpha=0.9)+#形状 scale_size_continuous(range = c(1, 15))+#气泡的相对大小 theme_bw()+ theme(panel.grid = element_blank(), #背景 axis.line=element_line(),#坐标轴的线设为显示 axis.text=element_text(color='black',size=12), legend.text = element_text(color='black',size=12), axis.title= element_text(size=12), axis.text.x=element_text(vjust = 1,hjust = 1), legend.key = element_blank())+ scale_fill_manual(values=c("#34a186","#f9cb45","#b5182b","#4cb1c4","#ab96d2"))+#指定颜色 labs(x = NULL, # 定义x轴文本 y = NULL)# 定义y轴文本
#背景色color <- colorRampPalette(brewer.pal(11,"BrBG"))(30)#添加背景grid.raster(alpha(color, 0.2), width = unit(1, "npc"), height = unit(1,"npc"), interpolate = T)

温馨提示

如果你喜欢本文,请分享到朋友圈,想要获得更多信息,请关注我。





您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存