其他
R可视化——棒棒糖&哑铃图绘图模板更新!
点击上方
“科研后花园”
关注我们
代码如下
1、加载R包
rm(list = ls())
setwd("D:\\桌面\\棒棒糖图&哑铃图")
# 加载R包
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="data.txt",sep="\t",header=T,check.names=FALSE)
3、颜色
#准备配色
col <- colorRampPalette(brewer.pal(11,"Spectral"))(21)
#背景色
color <- colorRampPalette(brewer.pal(11,"BrBG"))(30)
4、绘图
#绘图
p1 <- ggplot(df) +
geom_hline(yintercept = 120, lty=4,color = '#00a4e4', lwd=1) + #辅助线
geom_segment(aes(x=group, xend=group, y=120, yend=value2), color="#cf8d2e",size=1.5,lty=1) +
geom_point( aes(x=group, y=value2,fill=group), size=4,shape=21,color="black" ) +
scale_fill_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 = "none",
axis.title= element_text(size=12),
axis.text.x=element_text(angle = 45,vjust = 1,hjust = 1))+
labs(x=NULL,y=NULL)
p2 <- ggplot(df) +
geom_segment(aes(x=group, xend=group, y=value1,
yend=value2), color="#d4c99e",size=1.5) +#数据点之间的连线
geom_point( aes(x=group, y=value1), color='#ff9900', size=4 ) +#数据点1
geom_point( aes(x=group, y=value2), color='#146eb4', size=4 ) +#数据点2
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 = "none",
axis.title= element_text(size=12),
axis.text.x=element_text(angle = 45,vjust = 1,hjust = 1))+
labs(x=NULL,y=NULL)
#拼图
cowplot::plot_grid(p2,p1,ncol = 2)
#添加背景
grid.raster(alpha(color, 0.2),
width = unit(1, "npc"),
height = unit(1,"npc"),
interpolate = T)
温馨提示
如果你喜欢本文,请分享到朋友圈,想要获得更多信息,请关注我。