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

相关文章
|
JavaScript 前端开发 索引
vue--常用指令和事件修饰符(一)
vue--常用指令和事件修饰符
|
供应链 物联网 分布式数据库
区块链技术与智能合约开发的边界究竟在哪里?
【6月更文挑战第10天】本文探讨了区块链技术与智能合约的界限和挑战。区块链,本质是分布式数据库,以其不可篡改性和安全性在金融、供应链和物联网等领域广泛应用。智能合约,作为区块链上的自动执行代码,无需第三方介入,确保了执行的自动性和安全性。然而,技术上面临扩展性、性能和安全问题,法律与监管层则需解决合规监管和跨国法律协调的难题。尽管如此,随着技术进步和应用场景拓展,区块链与智能合约的潜力和未来前景依然广阔。
253 2
|
存储 人工智能 并行计算
强强联手!吉利星睿智算中心正式揭牌
近日,吉利星睿智算中心在湖州长兴正式揭牌。这是吉利有史以来设施规模最大、最复杂的信息化战略项目。
899 0
强强联手!吉利星睿智算中心正式揭牌
|
Web App开发 存储 自然语言处理
推荐一款价值几万元的免费开源GPTs导航!还可自定义数据源做成通用导航站!
推荐一款价值几万元的免费开源GPTs导航!还可自定义数据源做成通用导航站!
428 0
|
存储 Java 索引
Java 集合框架03---ArrayList的源码分析
上篇我们学习了Collection的相关源码,下面我们将继续学习List 家族中最常用的一个集合ArrayList。
249 0
Java 集合框架03---ArrayList的源码分析
|
机器学习/深度学习 编解码 监控
VDSR、DRRN、LapSRN、RCAN、DSRN…你都掌握了吗?一文总结超分辨率分析必备经典模型(二)(1)
VDSR、DRRN、LapSRN、RCAN、DSRN…你都掌握了吗?一文总结超分辨率分析必备经典模型(二)
790 0
|
Web App开发 Java 测试技术
JMeter进行WebSocket压力测试
之前两篇内容介绍了一下 WebSocket 和 SocketIO 的基础内容。之后用 Netty-SocketIO 开发了一个简单的服务端,支持服务端主动向客户端发送消息,同时也支持客户端请求,服务端响应方式。本文主要想了解一下服务端的性能怎么样,选择使用 JMeter 对 WebSocket 应用进行性能测试。
okcc呼叫中心使用SIP电话客户端时怎样设置?
okcc呼叫中心使用SIP电话客户端时怎样设置?

热门文章

最新文章