ggplot2包|柱形图
专题介绍:R是一种广泛用于数据分析和统计计算的强大语言,于上世纪90年代开始发展起来。得益于全世界众多 爱好者的无尽努力,大家继而开发出了一种基于R但优于R基本文本编辑器的R Studio(用户的界面体验更好)。也正是由于全世界越来越多的数据科学社区和用户对R包的慷慨贡献,让R语言在全球范围内越来越流行。其中一些R包,例如MASS,SparkR, ggplot2,使数据操作,可视化和计算功能越来越强大。R是用于统计分析、绘图的语言和操作环境。R是属于GNU系统的一个自由、免费、源代码开放的软件,它是一个用于统计计算和统计制图的优秀工具。R作为一种统计分析软件,是集统计分析与图形显示于一体的。它可以运行于UNIX、Windows和Macintosh的操作系统上,而且嵌入了一个非常方便实用的帮助系统,相比于其他统计分析软件,R的学术性开发比较早,适合生物学和医学等学术学科的科研人员使用。
【R语言】开通了R语言群,大家相互学习和交流,请扫描下方二维码,备注:R群,我会邀请你入群,一起进步和成长。
本文介绍ggplot2包绘制柱形图。柱形图是一种常用的数据可视化图形,使用垂直的柱子显示类别之间数值分布,x轴表示对比的分类维度,y轴表示相应的数值。柱形图的功能对比分类数据的数值大小,适合的数据是一个分类数据字段和一个连续数据字段,柱子的条数建议不要超过12条。若是条数太多,可以采用条形图(水平方向展示)或者把一些类别合并为一种情况来处理。
利用ggplot2包绘制柱形图,步骤如下:
首先,R包管理
if(!require("pacman")) {
install.packages("pacman")
require("pacman")
}
p_load(ggplot2, ggthemes, dplyr, readr, scales, forcats)
接下来,数据准备和加载
chilean_exports <- "year,product,export,percentage
2006,copper,4335009500,81
2006,others,1016726518,19
2007,copper,9005361914,86
2007,others,1523085299,14
2008,copper,6907056354,80
2008,others,1762684216,20
2009,copper,10529811075,81
2009,others,2464094241,19
2010,copper,14828284450,85
2010,others,2543015596,15
2011,copper,15291679086,82
2011,others,3447972354,18
2012,copper,14630686732,80
2012,others,3583968218,20
2013,copper,15244038840,79
2013,others,4051281128,21
2014,copper,14703374241,78
2014,others,4251484600,22
2015,copper,13155922363,78
2015,others,3667286912,22
"
exports_data <- read_csv(chilean_exports)
第三,数据理解
# 数据结构
str(exports_data)
# 数据概况
summary(exports_data)
# 数据检视
head(exports_data)
第四、逐步创建柱状图
1)基础柱形图
p1 <- ggplot(data = exports_data,
aes(x = year, y = export, fill = product)) +
geom_col()
p1
2)调整柱状的堆叠顺序
p2 <- ggplot(data = exports_data,
aes(x = year, y = export, fill = fct_rev(product))) +
geom_col()
p2
3)给柱子添加相应的数值
exports_data <- exports_data %>%
mutate(export_label = paste(round(export/1000000000,2), "B"))
p3 <- ggplot(data = exports_data,
aes(x = year, y = export, fill = fct_rev(product))) +
geom_col() +
geom_text(data = exports_data, aes(x = year, y = export,
label = export_label), size = 3)
p3
4)调整数据标签的位置
# 垂直居中
p4 <- ggplot(data = exports_data,
aes(x = year, y = export, fill = fct_rev(product))) +
geom_col() +
geom_text(aes(label = export_label),
position = position_stack(vjust = 0.5),
size = 3)
p4
5) 图例管理
exports_data <- exports_data %>%
mutate(product = factor(product, levels = c("copper","others"),
labels = c("Copper ","Pulp wood, Fruit, Salmon & Others")))
p5 <- ggplot(data = exports_data,
aes(x = year, y = export, fill = fct_rev(product))) +
geom_col() +
geom_text(aes(label = export_label),
position = position_stack(vjust = 0.5),
size = 3) + theme(legend.position = "bottom", legend.direction = "horizontal",
legend.title = element_blank()) +
theme(legend.position = "bottom", legend.direction = "horizontal",
legend.title = element_blank()) +
guides(fill = guide_legend(reverse = TRUE))
p5
6)调整x轴刻度
p6 <- p5 + scale_x_continuous(breaks = seq(2006,2015,1))
p6
7)添加轴标签和标题
p7 <- p6 + labs(title = "Composition of Exports to China ($)",
subtitle = "Source: The Observatory of Economic Complexity") +
labs(x = "Year", y = "USD million")
p7
8)柱子的填充色美化
fill <- c("#E1B378","#5F9EA0")
p8 <- p7 + scale_fill_manual(values = fill)
p8
9)配置主题
使用经济学杂志的主题
p_load(showtext)
font_add("Tahoma","Tahoma.ttf")
showtext_auto()
fill <- c("#00a3dc","#01526d")
p8 <- ggplot(aes(y = export, x = year, fill = fct_rev(product)), data = exports_data) +
geom_col() +
geom_text(aes(label = export_label), position = position_stack(vjust = 0.5),
colour = "white", size = 3, family = "Tahoma",
show.legend = F) +
scale_x_continuous(breaks = seq(2006,2015,1)) +
labs(title = "Composition of Exports to China ($)",
subtitle = "Source: The Observatory of Economic Complexity") +
labs(x = "Year", y = "USD million") +
theme_economist() +
scale_fill_manual(values = fill) +
theme(axis.line.x = element_line(size = .5, colour = "black"),
legend.position = "bottom",
legend.direction = "horizontal",
legend.title = element_blank(),
plot.title = element_text(family = "Tahoma"),
text = element_text(family = "Tahoma")) +
guides(fill = guide_legend(reverse = TRUE))
p8
附录:完整代码
###############
#柱形图
#2021-03-16
##############
# 1 R包管理
if(!require("pacman")) {
install.packages("pacman")
require("pacman")
}
p_load(ggplot2, ggthemes, dplyr, readr, scales, forcats)
# 2 数据准备和加载
chilean_exports <- "year,product,export,percentage
2006,copper,4335009500,81
2006,others,1016726518,19
2007,copper,9005361914,86
2007,others,1523085299,14
2008,copper,6907056354,80
2008,others,1762684216,20
2009,copper,10529811075,81
2009,others,2464094241,19
2010,copper,14828284450,85
2010,others,2543015596,15
2011,copper,15291679086,82
2011,others,3447972354,18
2012,copper,14630686732,80
2012,others,3583968218,20
2013,copper,15244038840,79
2013,others,4051281128,21
2014,copper,14703374241,78
2014,others,4251484600,22
2015,copper,13155922363,78
2015,others,3667286912,22
"
exports_data <- read_csv(chilean_exports)
# 3 数据理解
# 数据结构
str(exports_data)
# 数据概况
summary(exports_data)
# 数据检视
head(exports_data)
# 4 逐步创建柱形图
# 1)基础柱形图
p1 <- ggplot(data = exports_data,
aes(x = year, y = export, fill = product)) +
geom_col()
p1
# 2) 调整柱状堆叠顺序
p2 <- ggplot(data = exports_data,
aes(x = year, y = export, fill = fct_rev(product))) +
geom_col()
p2
# 3) 给柱子添加相应的数值
# 新增标签变量
exports_data <- exports_data %>%
mutate(export_label = paste(round(export/1000000000,2), "B"))
p3 <- ggplot(data = exports_data,
aes(x = year, y = export, fill = fct_rev(product))) +
geom_col() +
geom_text(data = exports_data, aes(x = year, y = export,
label = export_label), size = 3)
p3
# 4)调整数据标签的位置
# 垂直居中
p4 <- ggplot(data = exports_data,
aes(x = year, y = export, fill = fct_rev(product))) +
geom_col() +
geom_text(aes(label = export_label),
position = position_stack(vjust = 0.5),
size = 3)
p4
# 5) 图例管理
exports_data <- exports_data %>%
mutate(product = factor(product, levels = c("copper","others"),
labels = c("Copper ","Pulp wood, Fruit, Salmon & Others")))
p5 <- ggplot(data = exports_data,
aes(x = year, y = export, fill = fct_rev(product))) +
geom_col() +
geom_text(aes(label = export_label),
position = position_stack(vjust = 0.5),
size = 3) + theme(legend.position = "bottom", legend.direction = "horizontal",
legend.title = element_blank()) +
theme(legend.position = "bottom", legend.direction = "horizontal",
legend.title = element_blank()) +
guides(fill = guide_legend(reverse = TRUE))
p5
# 6) 调整x轴刻度
p6 <- p5 + scale_x_continuous(breaks = seq(2006,2015,1))
p6
# 7) 添加轴标签和标题
p7 <- p6 + labs(title = "Composition of Exports to China ($)",
subtitle = "Source: The Observatory of Economic Complexity") +
labs(x = "Year", y = "USD million")
p7
# 8) 柱子填充色美化
fill <- c("#E1B378","#5F9EA0")
p8 <- p7 + scale_fill_manual(values = fill)
p8
# 9)配置主题
# 使用经济学杂志主题
p_load(showtext)
font_add("Tahoma","Tahoma.ttf")
showtext_auto()
fill <- c("#00a3dc","#01526d")
p8 <- ggplot(aes(y = export, x = year, fill = fct_rev(product)), data = exports_data) +
geom_col() +
geom_text(aes(label = export_label), position = position_stack(vjust = 0.5),
colour = "white", size = 3, family = "Tahoma",
show.legend = F) +
scale_x_continuous(breaks = seq(2006,2015,1)) +
labs(title = "Composition of Exports to China ($)",
subtitle = "Source: The Observatory of Economic Complexity") +
labs(x = "Year", y = "USD million") +
theme_economist() +
scale_fill_manual(values = fill) +
theme(axis.line.x = element_line(size = .5, colour = "black"),
legend.position = "bottom",
legend.direction = "horizontal",
legend.title = element_blank(),
plot.title = element_text(family = "Tahoma"),
text = element_text(family = "Tahoma")) +
guides(fill = guide_legend(reverse = TRUE))
p8
好书推荐
3 推断统计与数据科学,moderndive和tidyverse包
公众号推荐
请关注“恒诺新知”微信公众号,感谢“R语言“,”数据那些事儿“,”老俊俊的生信笔记“,”冷🈚️思“,“珞珈R”,“生信星球”的支持!