其他
R绘图模板——气球图的绘制!
点击上方
“科研后花园”
关注我们
代码如下:
1、设置工作环境并加载所需R包:
rm(list=ls())#clear Global Environment
setwd('D:\\气球图')#设置工作路径
#加载所需R包
library(reshape2) # Flexibly Reshape Data: A Reboot of the Reshape Package
library(ggplot2) # Create Elegant Data Visualisations Using the Grammar of Graphics
library(RColorBrewer) # ColorBrewer Palettes
library(grid) # The Grid Graphics Package
library(scales) # Scale Functions for Visualization
2、加载数据并对数据进行处理:
#读取数据
df <- read.table(file="Genus.txt",sep="\t",header=T,check.names=FALSE,row.names = 1)
#转换数据
df$Tax=rownames(df)
df1=melt(df)
colnames(df1)=c("Tax","Samples","value")
3、绘图并对其进行美化:
#####绘图色生成
col <- colorRampPalette(brewer.pal(12,"Paired"))(11)
####绘图
ggplot()+
geom_point(df1,mapping = aes(x = Samples, y = Tax, size = value, fill=Samples),shape=21)+
scale_fill_manual(values = col)+
scale_size_continuous(range = c(0, 10))+
theme(panel.background = element_blank(),
legend.key = element_blank(),
axis.text = element_text(color = "black",size = 10),
panel.grid.major = element_line(color = "gray"),#网格线条颜色
panel.border = element_rect(color="black",fill=NA))+#边框色
labs(x=NULL,y=NULL)
#背景色
color <- colorRampPalette(brewer.pal(11,"BrBG"))(30)
#添加背景
grid.raster(alpha(color, 0.1),
width = unit(1, "npc"),
height = unit(1,"npc"),
interpolate = T)
温馨提示
如果你喜欢本文,请分享到朋友圈,想要获得更多信息,请关注我。