用R语言根据频数分布表绘制相应的条形图和帕累托图
时间: 2024-05-24 07:10:34 浏览: 163
如何用EXCEL绘制排列图
下面是使用R语言绘制频数分布表的条形图和帕累托图的代码示例:
```R
# 创建数据框
df <- data.frame(grade = c("A", "B", "C", "D", "E"),
frequency = c(8, 12, 10, 5, 3))
# 绘制条形图
library(ggplot2)
ggplot(df, aes(x = grade, y = frequency)) +
geom_bar(stat = "identity", fill = "#69b3a2") +
ggtitle("Grade Distribution") +
xlab("Grade") + ylab("Frequency")
# 绘制帕累托图
df <- df[order(-df$frequency), ]
df$cumulative <- cumsum(df$frequency) / sum(df$frequency)
ggplot(df, aes(x = grade)) +
geom_bar(aes(y = frequency), stat = "identity", fill = "#69b3a2") +
geom_line(aes(y = cumulative * max(frequency), group = 1), color = "red") +
geom_point(aes(y = cumulative * max(frequency)), color = "red", size = 3) +
ggtitle("Grade Distribution Pareto Chart") +
xlab("Grade") + ylab("Frequency / Cumulative Percentage")
```
结果:
![image-20211101142812669](https://i.loli.net/2021/11/01/76Rv5eJYfLdCkzg.png)![image-20211101142823914](https://i.loli.net/2021/11/01/KwTtjgJLzZ8Q2yn.png)
阅读全文