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)

直方图和概率密度图

## 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作为横轴

image.png
# 
ggplot(dat, aes(x=rating)) +
    geom_histogram(binwidth=.5, 
    colour="black", # 边框颜色 
    fill="white" #填充颜色
 )


image.png

ggplot(dat, aes(x=rating)) + geom_density() # 添加密度曲线

image.png
# 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")  # 重叠部分采用透明设置


image.png

添加一条均值线(红色部分)

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 mean
               color="red", linetype="dashed", size=1)


image.png

多组数据的直方图和密度图

# cond作为各组的分类,以颜色填充作为区别
# position的处理很重要,决定数据存在重叠是的处理方式 "identity" 不做处理,但是设置了透明
ggplot(dat, aes(x=rating, fill=cond)) +
    geom_histogram(binwidth=.5, alpha=.5, position="identity")


image.png
# Interleaved histograms
ggplot(dat, aes(x=rating, fill=cond)) +
    geom_histogram(binwidth=.5, position="dodge")


image.png
# dodge 表示重叠部分进行偏离
 
# 密度图
ggplot(dat, aes(x=rating, colour=cond)) + geom_density()


image.png
# 半透明的填充
ggplot(dat, aes(x=rating, fill=cond)) + geom_density(alpha=.3)

image.png
# Find the mean of each group
library(plyr)
# 以 cond 作为分组, 计算每组的rating的均值
cdat <- ddply(dat, "cond", summarise, rating.mean=mean(rating))
cdat

# 绘制两组数据的均值
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)


image.png

密度图

ggplot(dat, aes(x=rating, colour=cond)) +
geom_density() +
geom_vline(data=cdat, aes(xintercept=rating.mean, colour=cond),
linetype="dashed", size=1)

image.png

使用分面

# 按照 cond 进行分面处理, 上图为A,下图为B
ggplot(dat, aes(x=rating)) + geom_histogram(binwidth=.5, colour="black", fill="white") + 
    facet_grid(cond ~ .)


image.png
# 添加均值线
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")


image.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 作者:严涛浙江大学作物遗传育种在读研究生(生物信息学方向)伪码农,R语言爱好者,爱开源 ggplot2学习笔记之图...
    Dylan的迪阅读 2,704评论 0 6
  • 图形属性 x y xmin xmax ymin ymax xend yend weight color:轮廓色 f...
    Liam_ml阅读 7,367评论 0 5
  • 本文讲述如何利用ggplot2绘制密度图和直方图 准备数据: set.seed(1126) #设置随机种子,让结果...
    钿璎纍纍佩珊珊阅读 2,795评论 0 4
  • 写在前面 ggplot2 是一个功能强大且灵活的R包 ,由Hadley Wickham 编写,其用于生成优雅的图...
    Boer223阅读 28,310评论 0 67
  • 一.基本概念 "一张统计图形就是从数据到几何对象(geometric object, 缩写为geom, 包括点、线...
    apricoter阅读 53,248评论 6 173