DatistEQ之ggplot2单变量绘图

本文主要讲述使用ggplot2组件,进行单变量的绘图。

构造一个随机数据集。

#构造数据集,随机创建两列数据
set.seed(1234)
wdata <- data.frame(
  sex=factor(rep(c("F", "M"), each=200)),
  weight=c(rnorm(200, 55), rnorm(200, 58))
)
#wdata等价于前节点推送进来的数据框inputtable
前10行数据

先绘制一个图层a,后面逐步添加图层

a <- ggplot(wdata, aes(x=weight))

可能添加的图层有:

对于一个连续变量:

  • 面积图:geom_area()
  • 密度图:geom_density()
  • 点图:geom_dotplot()
  • 频率多边图:geom_freqpoly()
  • 直方图:geom_histogram()
  • 经验累积密度图:stat_ecdf()
  • QQ图:stat_qq()

对于一个离散变量:

  • 条形图:geom_bar()
单变量绘图

一、单变量:连续型

1. 面积图

a+geom_area(stat = "bin")
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
面积图

改变颜色

a+geom_area(aes(fill=sex), stat = "bin", alpha=0.6)+ theme_classic()
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
带颜色的面积图

注意:y轴默认为变量weight的数量即count,如果y轴要显示密度,可用以下代码:

a+geom_area(aes(y=..density..), stat = "bin")
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
密度面积图

可以通过修改不同属性如透明度、填充颜色、大小、线型等自定义图形:

2. 密度图

使用以下函数:

  • geom_density():绘制密度图
  • geom_vline():添加竖直线
  • scale_color_manual():手动修改颜色
a <- ggplot(wdata, aes(x=weight)) + geom_density()
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
密度图

根据sex修改颜色,将sex映射给line颜色

a <- ggplot(wdata, aes(x=weight)) 
a+geom_density(aes(color=sex))
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
带颜色的密度图

修改填充颜色以及透明度

a+geom_density(aes(fill=sex), alpha=0.4)
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
带颜色填充的密度图

添加均值线以及手动修改颜色

if (!require("plyr")) install.packages("plyr")
library(plyr)
mu <- ddply(wdata, "sex", summarise, grp.mean=mean(weight))

a+geom_density(aes(color=sex))+
  geom_vline(data=mu, aes(xintercept=grp.mean, color=sex), linetype="dashed")+
  scale_color_manual(values = c("red", "blue"))
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")

带均值线的密度图

3. 点图

a+geom_dotplot()
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
点图

将sex映射给颜色

a+geom_dotplot(aes(fill=sex))
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
带颜色的点图

手动修改颜色

a+geom_dotplot(aes(fill=sex))+
  scale_fill_manual(values=c("#999999", "#E69F00"))
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
修改点图的颜色

4. 频率多边图

a+geom_freqpoly()
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
频率多边图

y轴显示为密度

a+geom_freqpoly(aes(y=..density..)) + theme_minimal()
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
频率多边图

修改颜色以及线型

a+geom_freqpoly(aes(color=sex, linetype=sex))+theme_minimal()
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")

频率多边图

5. 直方图

header1("直方图")
a+geom_histogram()  
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
直方图

将sex映射给线颜色

a+geom_histogram(aes(color=sex), fill="white", position = "dodge")+theme_classic()
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
直方图

6. 经验累积密度图

a+stat_ecdf()
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
经验累积密度图

7. QQ图

ggplot(data = mtcars, aes(sample=mpg))+stat_qq()
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
QQ图

二、单变量:离散型

data(mpg)
b <- ggplot(mpg, aes(x=fl))
b+geom_bar()
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
条形图

修改填充颜色

b+geom_bar(fill="steelblue", color="black")+theme_classic()
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
带颜色的条形图

本文中涉及的所有代码:

#构造数据集
set.seed(1234)
wdata <- data.frame(
  sex=factor(rep(c("F", "M"), each=200)),
  weight=c(rnorm(200, 55), rnorm(200, 58))
)

output(wdata)

#一个变量:连续型
library(ggplot2)
a <- ggplot(wdata, aes(x=weight)) + geom_density()
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")

#面积图
header1("面积图")
a+geom_area(stat = "bin")
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")

a+geom_area(aes(fill=sex), stat = "bin", alpha=0.6)+ theme_classic()
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")

a+geom_area(aes(y=..density..), stat = "bin")
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")

#密度图
header1("密度图")
a <- ggplot(wdata, aes(x=weight)) + geom_density()
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")

#根据sex修改颜色,将sex映射给line颜色
a <- ggplot(wdata, aes(x=weight)) 
a+geom_density(aes(color=sex))
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")

#修改填充颜色以及透明度
a+geom_density(aes(fill=sex), alpha=0.4)
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")

#添加均值线以及手动修改颜色
if (!require("plyr")) install.packages("plyr")
library(plyr)
mu <- ddply(wdata, "sex", summarise, grp.mean=mean(weight))

a+geom_density(aes(color=sex))+
  geom_vline(data=mu, aes(xintercept=grp.mean, color=sex), linetype="dashed")+
  scale_color_manual(values = c("red", "blue"))
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")

#点图
header1("点图")
a+geom_dotplot()
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")

a+geom_dotplot(aes(fill=sex))
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")

#手动修改颜色
a+geom_dotplot(aes(fill=sex))+
  scale_fill_manual(values=c("#999999", "#E69F00"))
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")

#频率多边图
header1("频率多边图")
a+geom_freqpoly()
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")

#y轴显示为密度
a+geom_freqpoly(aes(y=..density..))+
  theme_minimal()
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")

#修改颜色以及线型
a+geom_freqpoly(aes(color=sex, linetype=sex))+
  theme_minimal()
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")

header1("直方图")
a+geom_histogram()  
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")

#将sex映射给线颜色
a+geom_histogram(aes(color=sex), fill="white", position = "dodge")+theme_classic()
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")

header1("经验累积密度图")
#经验累积密度图
a+stat_ecdf()
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")

header1("QQ图")
ggplot(data = mtcars, aes(sample=mpg))+stat_qq()
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")

header1("一个离散变量")
#加载数据集
data(mpg)
b <- ggplot(mpg, aes(x=fl))
b+geom_bar()
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")

#修改填充颜色
b+geom_bar(fill="steelblue", color="black")+theme_classic()
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")

header1("两个变量:x,y皆连续")
header2("散点图")
b <- ggplot(data = mtcars, aes(x=wt, y=mpg))
b+geom_point()
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")

#将变量cyl映射给点的颜色和形状
b + geom_point(aes(color = factor(cyl), shape = factor(cyl)))
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")

#自定义颜色
b+geom_point(aes(color=factor(cyl), shape=factor(cyl)))+
  scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9"))+theme_classic()
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
  
header2("平滑线")
b+geom_smooth()
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")

#散点图+回归线
b+geom_point()+
  geom_smooth(method = "lm", se=FALSE)#去掉置信区间
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")

#使用loess方法
b+geom_point()+
  geom_smooth(method = "loess")
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")  
  
#将变量映射给颜色和形状
b+geom_point(aes(color=factor(cyl), shape=factor(cyl)))+
  geom_smooth(aes(color=factor(cyl), shape=factor(cyl)), method = "lm", se=FALSE, fullrange=TRUE)
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")  

参考文献
1、https://blog.csdn.net/woodcorpse/article/details/106552735

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,701评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,649评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,037评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,994评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,018评论 6 395
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,796评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,481评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,370评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,868评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,014评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,153评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,832评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,494评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,039评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,156评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,437评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,131评论 2 356

推荐阅读更多精彩内容

  • 作者:严涛浙江大学作物遗传育种在读研究生(生物信息学方向)伪码农,R语言爱好者,爱开源 ggplot2学习笔记之图...
    wanghaihua888阅读 2,632评论 0 6
  • 简介 文章较长,点击直达我的博客,浏览效果更好。本文内容基本是来源于STHDA,这是一份十分详细的ggplot2使...
    taoyan阅读 51,160评论 7 159
  • 写在前面 ggplot2 是一个功能强大且灵活的R包 ,由Hadley Wickham 编写,其用于生成优雅的图...
    Boer223阅读 28,113评论 0 67
  • 1、简单折线图 折线图的x既可以对应离散性变量,也可以对应连续型变量 当x对应因子变量时,必须使用group=1映...
    100gle阅读 9,424评论 0 3
  • 一、直方图 之前在初学ggplot2包时,初步了解了ggplot_histgram()是针对一组连续型变量绘制频率...
    小贝学生信阅读 7,605评论 0 3