查看原文
其他

R绘图模板——瀑布图绘制!

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

点击上方

“科研后花园”

关注我们


代码如下:

1、加载R包

rm(list = ls())
#安装R包# install.packages("waterfalls")#加载R包library(waterfalls) # Create Waterfall Charts using 'ggplot2' Simplylibrary(ggthemes) # Extra Themes, Scales and Geoms for 'ggplot2'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<-data.frame( A=LETTERS[1:10], B=c(5,2,-3,-2,5,8,-4,3,6,-15))

3、设置颜色

#背景色color <- colorRampPalette(brewer.pal(11,"PuOr"))(30)col = ifelse(df$B>0, "#eb2226", "#00aaff")#自定义颜色

4、绘图

waterfall(values = df$B, #数值 labels = df$A,#标签 rect_width = 0.7,#柱子宽度 draw_lines = T,#是否显示矩形间的连线 linetype = 2,#矩形间连线类型 rect_border = "#333c41",#矩形边框颜色 fill_by_sign = F,#正值及负值是否具有相同颜色 fill_colours = col,#自定义颜色 calc_total = T,#是否显示终值 total_rect_color = "#2db928",#终值填充色 total_rect_text_color = "white",#终值标签颜色 total_axis_text = 'Total')+#终值标签设置 theme_tufte()+#主题 theme(axis.text=element_text(color='#333c41',size=12), legend.position = "none")+ labs(x=NULL,y=NULL)#去除轴标题#添加背景grid.raster(alpha(color, 0.2), width = unit(1, "npc"), height = unit(1,"npc"), interpolate = T)

温馨提示

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






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

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