其他
R可视化——点线图绘图模板更新!
点击上方
“科研后花园”
关注我们
开讲啦!
之前小编给大家整理过折线图的绘制代码,也教过大家如何让折线图曲线更平滑好看,今天这期推文是在前期代码基础上进行改进,使得绘制的图形稍微精美一点,下面就看看吧!
1、加载R包
rm(list=ls())
setwd("D:\\桌面\\点线图")
#加载R包
library(ggplot2) # Create Elegant Data Visualisations Using the Grammar of Graphics
library(ggalt) # Extra Coordinate Systems, 'Geoms', Statistical Transformations,
library(RColorBrewer) # ColorBrewer Palettes
library(grid) # The Grid Graphics Package
library(scales) # Scale Functions for Visualization
2、读取数据
data <- read.delim("data.txt",
header=T,
row.names=1,
sep="\t",
stringsAsFactors = FALSE,
check.names = FALSE)
#赋予因子水平
data$Genus<-factor(
data$Genus,
levels=c("Bacillus","Cronobacter",
"Enterobacterales",
"Klebsiella","Pantoea",
"Pseudomonas","Rosenbergiella"),
labels = c("Bacillus","Cronobacter",
"Enterobacterales",
"Klebsiella","Pantoea",
"Pseudomonas","Rosenbergiella"))
3、准备配色
col <- c("#85BA8F", "#A3C8DC",
"#349839","#EA5D2D",
"black","#F09594","#2072A8")
#背景色
color <- colorRampPalette(brewer.pal(11,"PuOr"))(30)
4、绘图
##绘图模板
ggplot(data=data,aes(x=Compartment,y=RA,
group=Genus,color=Genus))+
geom_point(size=3)+
labs(x="Compartments", y="Relative abundance (%)")+
geom_xspline(spline_shape = -0.25)+
scale_x_discrete(limits=c("RS","RE","VE","SE","LE","P"))+
scale_color_manual(values=col)+
theme_bw() +
theme(panel.grid=element_blank(),
axis.text=element_text(color='#333c41',size=10),
legend.text = element_text(color='#333c41',size=10),
legend.title = element_blank(),
legend.position = "bottom",
axis.title= element_text(size=12))
#添加背景
grid.raster(alpha(color, 0.2),
width = unit(1, "npc"),
height = unit(1,"npc"),
interpolate = T)
温馨提示
如果你喜欢本文,请分享到朋友圈,想要获得更多信息,请关注我。