柱状图又叫条形图,是数据展示最重要的一类统计图,数据分析结果展示中使用频率非常高,各类统计软件均能绘制。在R语言中,有很多包可绘制柱状图,比如graphics包barplot()函数和ggplot2包geom_bar()函数。 本文介绍ggplot2包的geom_bar()函数绘制柱状图。
geom_bar()函数的基本用法:
geom_bar(
mapping = NULL,#美学映射
data = NULL, #数据
stat = "count",
position = "stack", #位置调整
...,
width = NULL, #栏宽度
na.rm = FALSE, #是否删除缺失值
orientation = NA, #图层方向
show.legend = NA, #图例
inherit.aes = TRUE
)
案例
#基础绘图
g <- ggplot(mpg, aes(class))
g + geom_bar()
基础绘图
#使用 weight 参数来统计分组内 displ 变量值之和
g + geom_bar(aes(weight = displ))
image.png
#使用 geom_col 绘制条形图
df <- data.frame(trt = c("a", "b", "c"), outcome = c(2.3, 1.9, 3.2))
ggplot(df, aes(trt, outcome)) +
geom_col()
image.png
#设置柱状图的颜色
p1 <- g + geom_bar(color = 'blue', fill='white')
p2 <- g + geom_bar(aes(fill=class))
p3 <- g + geom_bar(aes(colour=class), fill='white')
p4 <- g + geom_bar(aes(fill=class)) +
scale_fill_manual(values = c("#8c510a", "#d8b365", "#f6e8c3",
"#c7eae5", "#5ab4ac", "#01665e", "#af8dc3"))
#使用plot_grid()函数组合图
library(cowplot)
plot_grid(p1, p2, p3, p4, labels = LETTERS[1:4], ncol = 2)
组合图
堆积条形图
g <- ggplot(mpg, aes(class))
g + geom_bar(aes(fill = drv))
堆积柱状图
#旋转后的堆积柱状图
ggplot(mpg, aes(y = class)) +
geom_bar(aes(fill = drv), position = position_stack(reverse = TRUE)) +
theme(legend.position = "top")
旋转后的堆积柱状图
百分比柱状图
g + geom_bar(aes(fill = drv),
position = 'fill')
百分比柱状图
分组柱状图
p1 <- g + geom_bar(aes(fill = drv), position = position_dodge())
p2 <- g + geom_bar(aes(fill = drv), position = position_dodge2())
plot_grid(p1, p2, labels = LETTERS[1:2], ncol = 2)
分组柱状图
#调整柱子的宽度
p1 <- g + geom_bar(aes(fill = drv), position = position_dodge(preserve = 'single'))
p2 <- g + geom_bar(aes(fill = drv), position = position_dodge2(preserve = 'single'))
plot_grid(p1, p2, labels = LETTERS[1:2], ncol = 2)
分组柱状图
#设置柱子之间的间距
p1 <- g + geom_bar(aes(fill = drv),
position = position_dodge(
preserve = 'single', width = 0.5))
p2 <- g + geom_bar(aes(fill = drv),
position = position_dodge(
preserve = 'single', width = 1))
p3 <- g + geom_bar(aes(fill = drv),
position = position_dodge2(
preserve = 'single', padding = 0.5))
p4 <- g + geom_bar(aes(fill = drv),
position = position_dodge2(
preserve = 'single', padding = 1.2))
plot_grid(p1, p2, p3, p4, labels = LETTERS[1:4], ncol = 2)
分组柱状图
#给每个柱子添加误差线
#这里我们借助%>%函数(tidyverse包)
library(tidyverse)#tidyverse包很强大,后期需要仔细研究
mpg %>%
group_by(class, drv) %>%
summarise(count = n()) %>%
ggplot(aes(class, count)) +
geom_col(aes(fill=drv), position = position_dodge2(preserve = 'single')) +
geom_errorbar(aes(ymin = count - 1, ymax = count + 1),
position = position_dodge2(preserve = 'single', padding = 0.5))
分组柱状图
#每个柱子添加数据
mpg %>%
group_by(class, drv) %>%
summarise(count = n()) %>%
ggplot(aes(class, count)) +
geom_col(aes(fill=drv), position = position_dodge2(preserve = 'single')) +
geom_text(aes(label=count),
position = position_dodge2(width = 0.8, preserve = 'single'),
vjust = -0.5, hjust = 0.5)
分组柱状图
参考文献
[1] https://www.r-graph-gallery.com/
[2] Robert I. Kabacoff (著). R语言实战(高涛/肖楠/陈钢 译). 北京: 人民邮电出版社.
[3] https://www.jianshu.com/p/65b8aacefa20
[4] https://zhuanlan.zhihu.com/p/37865149
[5] https://blog.csdn.net/qq_37395039/article/details/107517927
网友评论