R可视化学习(1)--直方图

简介: 本篇介绍如何使用R软件和ggplot2包来制作直方图,我们需要用到geom_histgramh函数,也可以用geom_vline函数去增加线条展示平均值。

本篇介绍如何使用R软件和ggplot2包来制作直方图,我们需要用到geom_histgramh函数,也可以用geom_vline函数去增加线条展示平均值。

6cd7a1a2174a2dee55b476423f51643.png

准备数据

set.seed(1234)
df <- data.frame(
  sex=factor(rep(c("F", "M"), each=200)),
  weight=round(c(rnorm(200, mean=55, sd=5), rnorm(200, mean=65, sd=5)))
  )
head(df)
##   sex weight
## 1   F     49
## 2   F     56
## 3   F     60
## 4   F     43
## 5   F     57
## 6   F     58

基础直方图

library(ggplot2)
# Basic histogram
ggplot(df, aes(x=weight)) + geom_histogram()
# Change the width of bins
ggplot(df, aes(x=weight)) + 
  geom_histogram(binwidth=1)
# Change colors
p<-ggplot(df, aes(x=weight)) + 
  geom_histogram(color="black", fill="white")
p

719bf85ad1e1a55a7379fe56920ff2f.png

增加平均值与密度图

# Add mean line
p+ geom_vline(aes(xintercept=mean(weight)),
            color="blue", linetype="dashed", size=1)
# Histogram with density plot
ggplot(df, aes(x=weight)) + 
 geom_histogram(aes(y=..density..), colour="black", fill="white")+
 geom_density(alpha=.2, fill="#FF6666")

f1ee94d5bc84ec93b25a15dd572be73.png

改变线形与颜色

# Change line color and fill color
ggplot(df, aes(x=weight))+
  geom_histogram(color="darkblue", fill="lightblue")
# Change line type
ggplot(df, aes(x=weight))+
  geom_histogram(color="black", fill="lightblue",
                 linetype="dashed")

2a9e26ccf29106b7f3ff294ec3c1323.png

分组展示

library(plyr)
mu <- ddply(df, "sex", summarise, grp.mean=mean(weight))
head(mu)
# Change histogram plot line colors by groups
ggplot(df, aes(x=weight, color=sex)) +
  geom_histogram(fill="white")
# 重叠 histograms
ggplot(df, aes(x=weight, color=sex)) +
  geom_histogram(fill="white", alpha=0.5, position="identity")
# 交错 histograms
ggplot(df, aes(x=weight, color=sex)) +
  geom_histogram(fill="white", position="dodge")+
  theme(legend.position="top")
# Add mean lines
p<-ggplot(df, aes(x=weight, color=sex)) +
  geom_histogram(fill="white", position="dodge")+
  geom_vline(data=mu, aes(xintercept=grp.mean, color=sex),
             linetype="dashed")+
  theme(legend.position="top")
p

9c34e193550e0a1b4bf4ce59e639730.png

d34b7709390585bf5b0be70e8c68c9c.png

自定义线条颜色

自定义填充color改为fill即可

# Use custom color palettes
p+scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9"))
# Use brewer color palettes
p+scale_color_brewer(palette="Dark2")
# Use grey scale
p + scale_color_grey() + theme_classic() +
  theme(legend.position="top")

db0118d743df4df9c7a3ffd0d8c5159.png

自定义主题与文本

# Basic histogram
ggplot(df, aes(x=weight, fill=sex)) +
  geom_histogram(fill="white", color="black")+
  geom_vline(aes(xintercept=mean(weight)), color="blue",
             linetype="dashed")+
  labs(title="Weight histogram plot",x="Weight(kg)", y = "Count")+
  theme_classic()
# Change line colors by groups
ggplot(df, aes(x=weight, color=sex, fill=sex)) +
  geom_histogram(position="identity", alpha=0.5)+
  #geom_density(alpha=0.6)+
  geom_vline(data=mu, aes(xintercept=grp.mean, color=sex),
             linetype="dashed")+
  scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9"))+
  scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))+
  labs(title="Weight histogram plot",x="Weight(kg)", y = "Count")+
  theme_classic()
  p<-ggplot(df, aes(x=weight, color=sex)) +
  geom_histogram(fill="white", position="dodge")+
  geom_vline(data=mu, aes(xintercept=grp.mean, color=sex),
             linetype="dashed")
# Continuous colors
p + scale_color_brewer(palette="Paired") + 
  theme_classic()+theme(legend.position="top")
# Discrete colors
p + scale_color_brewer(palette="Dark2") +
  theme_minimal()+theme_classic()+theme(legend.position="top")
# Gradient colors
p + scale_color_brewer(palette="Accent") + 
  theme_minimal()+theme(legend.position="top")

e58e81d5654a96edfc17a5f41e8126f.png

参考链接: http://www.sthda.com/english/wiki/ggplot2-box-plot-quick-start-guide-r-software-and-data-visualization

相关文章
|
2月前
|
数据可视化 数据挖掘
使用R语言进行多维缩放分析
【4月更文挑战第27天】本文介绍了R语言中的多维缩放分析(MDS)技术,用于高维数据的可视化。MDS通过映射数据点到低维空间保持距离或相似性,帮助理解数据结构。R中的`cmdscale`和`isoMDS`函数可用于构建MDS模型,而`dist`计算距离矩阵。通过实例展示了如何分析消费者对品牌评价,`stressplot`和`procrustes`函数则用于模型解释和验证。R还支持经典MDS、度量MDS和非度量MDS等高级主题,为数据探索提供强大工具。
|
2月前
|
计算机视觉 Python
直方图基础
直方图基础。
17 1
|
26天前
|
自然语言处理 数据可视化 Python
卡方分布和 Zipf 分布模拟及 Seaborn 可视化教程
卡方分布是统计学中的一种连续概率分布,用于假设检验,形状由自由度(df)决定。自由度越大,分布越平缓。NumPy的`random.chisquare()`可生成卡方分布随机数。Seaborn能可视化卡方分布。练习包括模拟不同自由度的卡方分布、进行卡方检验。瑞利分布描述信号处理中幅度分布,参数为尺度(scale)。Zipf分布常用于自然语言等幂律特征数据,参数a控制形状。NumPy的`random.zipf()`生成Zipf分布随机数。
22 0
|
2月前
|
数据可视化
R语言用igraph绘制网络图可视化
R语言用igraph绘制网络图可视化
|
2月前
如何用R语言绘制生成正态分布图表
如何用R语言绘制生成正态分布图表
|
11月前
|
数据可视化 数据挖掘 Linux
数据可视化丨优雅的绘制带显著性标记的箱线散点图,主要使用ggsignif和ggplot2
数据可视化丨优雅的绘制带显著性标记的箱线散点图,主要使用ggsignif和ggplot2
|
移动开发 数据可视化 HTML5
R可视化学习—词云图
词云。又称文字云。“词云”就是通过形成“关键词云层”或“关键词渲染”,对网络文本中出现频率较高的“关键词”的视觉上的突出,它会过滤掉大量的文本信息,使浏览网页者只要一眼扫过文本就可以领略文本的主旨。
130 0
|
数据可视化
R可视化学习(2)--箱线图
箱线图由箱和“须”(whisker)两部分组成。箱的范围是从数据的下四分位数到上四分位数,也就是常说的四分位距(IQR)。箱的中间有一条表示中位数,或者说50%分位数的线。须则是从箱子的边缘出发延伸至1.5倍四分位距内的最远的点。如果图中有超过须的数据点,则其被视为异常值,并以点来表示。如下图使用偏态的数据展示了直方图、密度曲线和箱线图之间的关系。
86 0
|
数据可视化 Python
Py之seaborn:数据可视化seaborn库(二)的组合图可视化之密度图/核密度图分布可视化、箱型图/散点图、小提琴图/散点图组合可视化的简介、使用方法之最强攻略(建议收藏)
Py之seaborn:数据可视化seaborn库(二)的组合图可视化之密度图/核密度图分布可视化、箱型图/散点图、小提琴图/散点图组合可视化的简介、使用方法之最强攻略(建议收藏)
Py之seaborn:数据可视化seaborn库(二)的组合图可视化之密度图/核密度图分布可视化、箱型图/散点图、小提琴图/散点图组合可视化的简介、使用方法之最强攻略(建议收藏)
|
数据可视化
R实战|卡方检验及其可视化
R实战|卡方检验及其可视化
539 0
R实战|卡方检验及其可视化