查看原文
其他

R绘图模板——南丁格尔玫瑰图的绘制!

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

点击上方

“科研后花园”

关注我们


代码如下

1、设置工作目录并加载所需R包:

#设置工作环境rm(list=ls())setwd("D:\\桌面\\环状柱形图")
#加载相关R包library(ggplot2) # Create Elegant Data Visualisations Using the Grammar of Graphicslibrary(ggthemes) # Extra Themes, Scales and Geoms for 'ggplot2'library(RColorBrewer) # ColorBrewer Paletteslibrary(grid) # The Grid Graphics Packagelibrary(scales) # Scale Functions for Visualization

2、加载数据(随机编写,无实际意义)

#加载数据df <- read.table("data1.txt",header = T, check.names = F)

3、配色:

#配色col <- colorRampPalette(brewer.pal(9,"Set1"))(7)#背景色color <- colorRampPalette(brewer.pal(11,"BrBG"))(30)

4、根据数据绘图:

ggplot(df, aes(x = sample, y = value, fill = sample)) + geom_bar(stat = "identity", color = "white", lwd = 1, show.legend = FALSE,width = 0.6)+ geom_text(aes(y=value+5,label=value,color=sample))+ scale_fill_manual(values = col)+ scale_color_manual(values = col)+ theme_pander()+ coord_polar()+ theme(axis.text.y = element_blank(), axis.ticks.y =element_blank(), axis.text.x = element_text(color='black',size=15), legend.position = "none", panel.grid.major.x = element_blank())+ labs(y=NULL,x=NULL)#添加背景grid.raster(alpha(color, 0.2), width = unit(1, "npc"), height = unit(1,"npc"), interpolate = T)


5、根据不同分组进行着色:

#绘图ggplot(df, aes(x = sample, y = value, fill = group)) + geom_bar(stat = "identity", color = "white", lwd = 1, show.legend = T,width = 0.6)+ geom_text(aes(y=value+5,label=value,color=group),show.legend = F)+ scale_fill_manual(values = col)+ scale_color_manual(values = col)+ theme_pander()+ coord_polar()+ theme(axis.text.y = element_blank(), axis.ticks.y =element_blank(), axis.text.x = element_text(color='black',size=15), legend.position = "right", panel.grid.major.x = element_blank())+ labs(y=NULL,x=NULL)#添加背景grid.raster(alpha(color, 0.2), width = unit(1, "npc"), height = unit(1,"npc"), interpolate = T)

温馨提示

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





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

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