查看原文
其他

R可视化——棒棒糖&哑铃图绘图模板更新!

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

点击上方

“科研后花园”

关注我们


代码如下

1、加载R包

rm(list = ls())setwd("D:\\桌面\\棒棒糖图&哑铃图")
# 加载R包library(ggplot2) # Create Elegant Data Visualisations Using the Grammar of Graphicslibrary(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、颜色

#准备配色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)

温馨提示

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







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

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