以下是R绘制瀑布图的小示例。
1.创建数据框;
library(ggalluvial)
##创建数据框
group <- LETTERS[1:15]
value <- c(600, 1680, 2000,3213,3500,3121,2740,-887
-1544, -1010, -2640,-2400,-1495,-954,1541,501)
df <- data.frame(x = group, y = value)
2.简单绘图;
library(waterfalls)
waterfall(df)
###计算数值总和并展现在图中
waterfall(df, calc_total = TRUE)
3.调整相关参数;
library(waterfalls)
#调整矩形的宽度
waterfall(df, rect_width = 0.4,calc_total = TRUE)
#删除矩形之间的连线
waterfall(df, draw_lines = FALSE,calc_total = TRUE)
#更改行类型
waterfall(df, linetype = 1,calc_total = TRUE) #保持矩形之间的直线
4.更改默认主题;
library(waterfalls)
library(ggplot2)
waterfall(df) +
theme_minimal()
5.更改颜色:
1)fill_by_sign = TRUE函数默认正值和负值是相同的颜色。如果不想如此,可设置fill_colours参数自定义颜色
library(waterfalls)
waterfall(df,
fill_by_sign = FALSE,
fill_colours = 1:15)
2)更改总矩阵的颜色:正值、负值、合计总值
library(waterfalls)
waterfall(df, calc_total = TRUE,
total_rect_color = "#3C5488B2", #更改矩形颜色
total_rect_text_color = "white", #更改文本颜色
rect_border = NA) #更改边框颜色,若去除边框则设置为NA
关注“作图帮”公众号,免费分享绘图代码与示例数据~
转载:https://blog.csdn.net/weifanbio/article/details/117280317
查看评论