微课|ggplot2:条形图

2017 年 8 月 2 日 数萃大数据 周世荣
P

ROBLEM

今日问题

  • 使用ggplot2绘制条形图图.

  • 条形图排序.

  • 给条形图添加标签.

  • scale_XX_XX系列使用方法.

  • 一些常用主题模块介绍.

  • 绘制多变量条形图.

  • 绘制具有误差条的条形图



数据准备

这次微课使用base数据集中ToothGrowth,为便于讨论数据集被构造成df,df2与df3三个数据框.其中df用来绘制基本条形图,df2用来绘制分组条形图,df3用来绘制具有误差条的条形图.

#使用到R包
library(ggplot2)library(plyr)
#数据集一
df <- data.frame(dose=c("D0.5", "D1", "D2"),                 len=c(10, 4.2, 29.5)) head(df)
#********************************************************************
#数据集二
df2 <- data.frame(supp=rep(c("VC", "OJ"), each=3),                  dose=rep(c("D0.5", "D1", "D2"),2),                  len=c(6.8, 15, 33, 4.2, 10, 29.5)) head(df2)
#*********************************************************************
#数据集三
data_summary <- function(data, varname, groupnames)
{  require(plyr)  summary_func <- function(x, col){    c(mean = mean(x[[col]], na.rm=TRUE),      sd = sd(x[[col]], na.rm=TRUE))  }  data_sum<-ddply(data, groupnames, .fun=summary_func, varname)  data_sum <- rename(data_sum, c("mean" = varname))  return(data_sum) } df3 <- data_summary(ToothGrowth, varname="len",                    groupnames=c("supp", "dose")) df3$dose=as.factor(df3$dose) head(df3)
基本命令

绘制条形图的基方法是ggplot(…)+geon_bar(…),具体看下面代码块及图形

#创建画板
f <- ggplot(df, aes(x = dose, y = len))
#基本条形图
f + geom_bar(stat = "identity")

排序

条形图排序使用reorder(Var1, Var2),其中Var1为字符型变量,Var2为数值型变量.默认为升序,若使用降序排列只需修改为reorder(Var1, -Var2)

f <- ggplot(df,aes(reorder(dose,len),y=len))
f + geom_bar(stat = "identity")


添加标签

使用geom_text()函数添加标签,然后通过vjust对标签微调,在使用colour,sizede等自定义标签.

f + geom_bar(stat="identity", fill="steelblue",colour='white')+
  geom_text(aes(label=len), vjust=1.6, color="white", size=3.5)+
  theme_minimal()



改变填充色

我想通过这个例子来说明scale_XXX_XXX系列函数. 在下面代码块的第一行,我们将dose映射给了填充色参数fill,下面我们通过scale_fill_XXX来改变填充色.主要有三种scale_fill_manual自定义颜色;scale_fill_brewer调用RColorBrewer包调色板;scale_fill_grey使用ggplot2包自带的灰色调色板.最后使用的theme_minimal()是ggplot2封装的一个主题,称为最简主题,此外还有theme_gray, theme_classic, theme_bw, theme_light 等9个主题.有兴趣的同学也可以使用ggplot2的扩展包ggthemes,提供了经济学人,华尔街日报,google,EXCEL等14个主题.

f + geom_bar(aes(fill = dose), stat="identity") +  
 #scale_fill_manual(values = c("#999999", "#E69F00", "#56B4E9"))+  #scale_fill_brewer(palette="Dark2")+  scale_fill_grey()+  theme_minimal()



多变量条形图

多变量条形图中主要使用的是并列条形图和堆叠条形图, 并列条形图为默认的方式,堆叠条形图需要自定义数据类型,另外堆叠条形图标签的添加也需要一些简单的数学运算,具体过程见下面代码块.

#并列条形图
ggplot(data=df2, aes(x=dose, y=len, fill=supp)) +  geom_bar(stat="identity", position = position_dodge())+   scale_fill_grey()+  geom_text(aes(label = len), vjust = 1.6, color = "red",            position = position_dodge(0.9), size = 3.5)+  theme_minimal()
#*************************************************************
#堆叠条形图
#按剂量和注射方式对df分组
df_sorted <- arrange(df2, dose, supp) head(df_sorted)
#自定义函数
fun=function(x){  tol=cumsum(x)  re=(tol-c(0,tol[-length(tol)]))/2+c(0,tol[-length(tol)])  return(re) } df_cumsum <- ddply(df_sorted, "dose", transform,                   label_ypos=cumsum(len)) head(df_cumsum) ggplot(data=df_cumsum, aes(x = dose, y = len, fill = supp)) +  geom_bar(stat = "identity")+  scale_fill_grey()+  geom_text(aes(y = label_ypos, label = len), vjust=1.6,            color = "red", size = 3.5)+  theme_minimal()


添加误差条

使用geom_errorbar()函数给条形图添加误差条,关键是数据集的构造,具体过程大家可以参照df3的狗造过程以及下面的代码块

p <- ggplot(df3, aes(x=dose, y=len, fill=supp)) +
  geom_bar(stat="identity", position=position_dodge()) +
  geom_errorbar(aes(ymin=len-sd, ymax=len+sd), width=.2,
                position=position_dodge(.9),colour='red')
p + labs(title="添加误差条",caption='@数萃大数据',
         x="剂量 (mg)", y = "长度")+
  scale_fill_manual(values=c('gray20','lightgray'))+
  theme_minimal()


推荐阅读

微课|ggplot2: 基础知识

微课|ggplot2: 散点图

微课|ggplot2: 折线图

微课|R基础绘图:箱线图

【Python微课】系列之一:Matplotlib基础绘图

【统计思想之十六】—— 古人学问无遗力,少壮工夫老始成。

易图秒懂の人工智能诞生

【案例分析】NBA球员投篮数据分析与可视化

更多微课请关注【数萃大数据】公众号,点击学习园地—可视化


欢迎大家关注微信公众号:数萃大数据



课程公告

python大数据分析培训班【杭州站】

时间:2017年8月18日-22日

地点:杭州亚朵吴酒店

更多详情,请扫描下面二维码

网络爬虫与文本挖掘培训班【宁波站】

时间:2017年9月23日-25日

地点:维也纳国际酒店(机场店)

更多详情,请扫描下面二维码

登录查看更多
0

相关内容

【2020新书】从Excel中学习数据挖掘,223页pdf
专知会员服务
93+阅读 · 2020年6月28日
【2020新书】使用高级C# 提升你的编程技能,412页pdf
专知会员服务
60+阅读 · 2020年6月26日
【干货书】用于概率、统计和机器学习的Python,288页pdf
专知会员服务
291+阅读 · 2020年6月3日
Python地理数据处理,362页pdf,Geoprocessing with Python
专知会员服务
114+阅读 · 2020年5月24日
【干货书】R语言书: 编程和统计的第一课程,
专知会员服务
115+阅读 · 2020年5月9日
【图神经网络(GNN)结构化数据分析】
专知会员服务
116+阅读 · 2020年3月22日
【康奈尔大学】度量数据粒度,Measuring Dataset Granularity
专知会员服务
13+阅读 · 2019年12月27日
Cayley图数据库的可视化(Visualize)
Python开发者
5+阅读 · 2019年9月9日
LeetCode的C++ 11/Python3 题解及解释
专知
16+阅读 · 2019年4月13日
Python奇淫技巧,5个数据可视化工具
机器学习算法与Python学习
7+阅读 · 2019年4月12日
ggstance:ggplot2的水平版本
R语言中文社区
5+阅读 · 2017年11月17日
【LeetCode 500】关关的刷题日记27 Keyboard Row
专知
3+阅读 · 2017年11月5日
Python3爬虫之入门和正则表达式
全球人工智能
7+阅读 · 2017年10月9日
使用 Python 绘制《星战》词云
Datartisan数据工匠
3+阅读 · 2017年8月31日
Arxiv
10+阅读 · 2020年4月5日
Arxiv
5+阅读 · 2018年3月28日
Arxiv
3+阅读 · 2018年2月24日
VIP会员
相关资讯
Cayley图数据库的可视化(Visualize)
Python开发者
5+阅读 · 2019年9月9日
LeetCode的C++ 11/Python3 题解及解释
专知
16+阅读 · 2019年4月13日
Python奇淫技巧,5个数据可视化工具
机器学习算法与Python学习
7+阅读 · 2019年4月12日
ggstance:ggplot2的水平版本
R语言中文社区
5+阅读 · 2017年11月17日
【LeetCode 500】关关的刷题日记27 Keyboard Row
专知
3+阅读 · 2017年11月5日
Python3爬虫之入门和正则表达式
全球人工智能
7+阅读 · 2017年10月9日
使用 Python 绘制《星战》词云
Datartisan数据工匠
3+阅读 · 2017年8月31日
Top
微信扫码咨询专知VIP会员