ggplot2–绘制分布图
本文更新地址: http://blog.csdn.net/tanzuozhev/article/details/51106291
本文在 http://www.cookbook-r.com/Graphs/Plotting_distributions_(ggplot2)/ 的基础上加入了自己的理解
生成绘图数据
set.seed(1234)
dat <- data.frame(cond = factor(rep(c("A","B"), each=200)), rating = c(rnorm(200),rnorm(200, mean=.8)))
# View first few rows
head(dat)
## cond rating
## 1 A -1.2070657
## 2 A 0.2774292
## 3 A 1.0844412
## 4 A -2.3456977
## 5 A 0.4291247
## 6 A 0.5060559
library(ggplot2)
直方图和概率密度图
## Basic histogram from the vector "rating". Each bin is .5 wide.
## These both result in the same output:
ggplot(dat, aes(x=rating)) + geom_histogram(binwidth=.5) # rating作为横轴
#
ggplot(dat, aes(x=rating)) +geom_histogram(binwidth=.5, colour="black", # 边框颜色 fill="white" #填充颜色)
ggplot(dat, aes(x=rating)) + geom_density() # 添加密度曲线
# Histogram overlaid with kernel density curve
ggplot(dat, aes(x=rating)) + geom_histogram(aes(y=..density..), # 这一步很重要,使用density代替y轴binwidth=.5,colour="black", fill="white") +geom_density(alpha=.2, fill="#FF6666") # 重叠部分采用透明设置
添加一条均值线(红色部分)
ggplot(dat, aes(x=rating)) +geom_histogram(binwidth=.5, colour="black", fill="white") +geom_vline(aes(xintercept=mean(rating, na.rm=T)), # Ignore NA values for meancolor="red", linetype="dashed", size=1)
多组数据的直方图和密度图
# cond作为各组的分类,以颜色填充作为区别
# position的处理很重要,决定数据存在重叠是的处理方式 "identity" 不做处理,但是设置了透明
ggplot(dat, aes(x=rating, fill=cond)) +geom_histogram(binwidth=.5, alpha=.5, position="identity")
# Interleaved histograms
ggplot(dat, aes(x=rating, fill=cond)) +geom_histogram(binwidth=.5, position="dodge")
# dodge 表示重叠部分进行偏离# 密度图
ggplot(dat, aes(x=rating, colour=cond)) + geom_density()
# 半透明的填充
ggplot(dat, aes(x=rating, fill=cond)) + geom_density(alpha=.3)
Add lines for each mean requires first creating a separate data frame with the means:
# Find the mean of each group
library(plyr)
# 以 cond 作为分组, 计算每组的rating的均值
cdat <- ddply(dat, "cond", summarise, rating.mean=mean(rating))
cdat
## cond rating.mean
## 1 A -0.05775928
## 2 B 0.87324927
# 绘制两组数据的均值
ggplot(dat, aes(x=rating, fill=cond)) +geom_histogram(binwidth=.5, alpha=.5, position="identity") +geom_vline(data=cdat, aes(xintercept=rating.mean, colour=cond),linetype="dashed", size=1)
# 密度图
ggplot(dat, aes(x=rating, colour=cond)) +geom_density() +geom_vline(data=cdat, aes(xintercept=rating.mean, colour=cond),linetype="dashed", size=1)
使用分面
按照 cond 进行分面处理, 上图为A,下图为B
# 按照 cond 进行分面处理, 上图为A,下图为B
ggplot(dat, aes(x=rating)) + geom_histogram(binwidth=.5, colour="black", fill="white") + facet_grid(cond ~ .)
# 添加均值线
ggplot(dat, aes(x=rating)) + geom_histogram(binwidth=.5, colour="black", fill="white") + facet_grid(cond ~ .) +geom_vline(data=cdat, aes(xintercept=rating.mean),linetype="dashed", size=1, colour="red")
箱线图
# A basic box plot
ggplot(dat, aes(x=cond, y=rating)) + geom_boxplot()
# cond作为填充颜色的分类
ggplot(dat, aes(x=cond, y=rating, fill=cond)) + geom_boxplot()
# The above adds a redundant legend. With the legend removed:
ggplot(dat, aes(x=cond, y=rating, fill=cond)) + geom_boxplot() +guides(fill=FALSE) # 关闭图例
# With flipped axes
ggplot(dat, aes(x=cond, y=rating, fill=cond)) + geom_boxplot() + guides(fill=FALSE) + coord_flip() # x轴 y轴翻转
使用 `stat_summary’ 添加均值
# Add a diamond at the mean, and make it larger
ggplot(dat, aes(x=cond, y=rating)) + geom_boxplot() +stat_summary(fun.y=mean, geom="point", shape=5, size=4)
ggplot2–绘制分布图相关推荐
- R语言ggplot2可视化:使用ggplot2绘制按时间顺序排列的时间线图(chronological timeline plot)
R语言ggplot2可视化:使用ggplot2绘制按时间顺序排列的时间线图(chronological timeline plot) 目录 R语言ggplot
- R语言R原生及可视化包ggplot2绘制并排的箱图实战(Side-by-Side Boxplots)
R语言R原生及可视化包ggplot2绘制并排的箱图实战(Side-by-Side Boxplots) 目录 R语言R原生及可视化包ggplot2绘制并排的箱图实战(Side-by-Side Boxpl ...
- R语言使用ggplot2绘制带有边缘直方图的散点图实战
R语言使用ggplot2绘制带有边缘直方图的散点图实战 目录 R语言使用ggplot2绘制带有边缘直方图的散点图实战
- R语言可视化包ggplot2绘制分组的条形图(bar plot、柱状图)实战:多变量柱状图
R语言可视化包ggplot2绘制分组的条形图(bar plot.柱状图)实战:多变量柱状图 目录
- R语言可视化包ggplot2绘制线性回归模型曲线实战( Linear Regression Line)
R语言可视化包ggplot2绘制线性回归模型曲线实战( Linear Regression Line) 目录 R语言可视化包ggplot2绘制线性回归模型曲线实战( Linear Regression ...
- R语言可视化包ggplot2绘制分组回归线实战(Regression Line by Group)
R语言可视化包ggplot2绘制分组回归线实战(Regression Line by Group) 目录 R语言可视化包ggplot2绘制分组回归线实战(Regression Line by Grou ...
- R语言可视化包ggplot2绘制平滑曲线、回归线实战:geom_smooth() 函数
R语言可视化包ggplot2绘制平滑曲线.回归线实战:geom_smooth() 函数 目录 R语言可视化包ggplot2绘制平滑曲线.回归线实战:geom_smooth() 函数
- R语言可视化包ggplot2绘制甘特图(gantt chart)实战
R语言可视化包ggplot2绘制甘特图(gantt chart)实战 目录 R语言可视化包ggplot2绘制甘特图(gantt chart)实战 #仿真数据
- R语言可视化包ggplot2绘制饼图(pie chart)实战
R语言可视化包ggplot2绘制饼图(pie chart)实战 目录 R语言可视化包ggplot2绘制饼图(pie chart)实战 #ggplot2绘制一个基本饼图
最新文章
- 我在兰亭这三年之大促的那些事儿
- python中的encode()和decode()函数_python里面的encode和decode函数
- 空间谱专题03:时空特性与采样定理
- 如何在PowerPoint演示文稿中使用iTunes音乐
- C# mysql 插入数据,中文乱码
- 用户控件事件使用delegate
- ModelMaker的教學網站
- PHP - ReflectorClass 反射类的作用
- (简单)华为Nova青春 WAS-AL00的USB调试模式在哪里开启的流程
- js sdk demo java_微信JS-SDK DEMO页面和示例代码
- 异速联显示连接服务器失败,异速联客户端登陆时正在连接服务器
- EDA课程设计(设计一个4时隙的时分复用模块)(岭)
- 如何选择毕业设计的题目?
- 双眼融合训练一个月_双眼视觉是什么?为什么要进行视功能训练?
- VS2015自定义编程背景
- 竖流式沉淀池集水槽设计计算_竖流沉淀池设计计算书.
- JQ实现谷歌小恐龙小游戏
- 用php计算身体质量指数,BMI计算器,身体质量指数BMI在线计算
- html 怎么转换,将 HTML 转换成任意你想要的形式!
- 第十七章 OAuth2集成——《跟我学Shiro》