利用R绘制条形图

利用R绘制条形图

在R中绘制条形图主要使用barplot()函数(基础绘图)和ggplot2包。下面我将展示多种类型的条形图及其实现方法。

欢迎大家在评论区留言或私信,交流学习心得或学习R的过程中遇到的问题。感谢大家的支持和关注,您的支持是我创作的最大动力。

目录

1.基础数据准备

go 复制代码
# 安装和加载必要的包(如果前期已安装可忽略)
#install.packages("ggplot2")
#install.packages("dplyr")
#install.packages("reshape2")

library(ggplot2)
library(dplyr)
library(reshape2)

# 创建示例数据
set.seed(123)
data <- data.frame(
  Category = c("A", "B", "C", "D", "E"),
  Value1 = c(23, 45, 56, 34, 67),
  Value2 = c(34, 38, 49, 28, 52),
  Group = c("X", "X", "Y", "Y", "Z")
)

2.使用barplot绘图

  • 简单条形图
go 复制代码
# 垂直条形图
barplot(data$Value1, 
        names.arg = data$Category,
        main = "简单垂直条形图",
        xlab = "类别",
        ylab = "数值",
        col = "skyblue")

出图效果如下:

  • 水平条形图
go 复制代码
# 水平条形图
barplot(data$Value1, 
        names.arg = data$Category,
        main = "简单水平条形图",
        xlab = "数值",
        ylab = "类别",
        col = "lightgreen",
        horiz = TRUE)

出图效果如下:

  • 分组条形图
go 复制代码
# 准备矩阵数据
matrix_data <- as.matrix(data[, c("Value1", "Value2")])
rownames(matrix_data) <- data$Category

# 分组条形图
barplot(matrix_data, 
        beside = TRUE,
        main = "分组条形图",
        xlab = "类别",
        ylab = "数值",
        col = c("skyblue", "lightcoral"),
        legend.text = c("Value1", "Value2"))

出图效果如下:

  • 堆叠条形图
go 复制代码
# 堆叠条形图
barplot(matrix_data, 
        beside = FALSE,
        main = "堆叠条形图",
        xlab = "类别",
        ylab = "数值",
        col = c("skyblue", "lightcoral"),
        legend.text = c("Value1", "Value2"))

出图效果如下:

3.使用ggplot2绘图

  • 简单条形图
go 复制代码
# 基本条形图
ggplot(data, aes(x = Category, y = Value1)) +
  geom_bar(stat = "identity", fill = "steelblue") +
  labs(title = "ggplot2 - 简单条形图", 
       x = "类别", y = "数值") +
  theme_minimal()

出图效果如下:

  • 水平条形图
go 复制代码
ggplot(data, aes(x = reorder(Category, Value1), y = Value1)) +
  geom_bar(stat = "identity", fill = "coral") +
  coord_flip() +
  labs(title = "ggplot2 - 水平条形图", 
       x = "类别", y = "数值") +
  theme_minimal()

出图效果如下:

  • 分组条形图
go 复制代码
data_long <- melt(data[, 1:3], id.vars = "Category")

ggplot(data_long, aes(x = Category, y = value, fill = variable)) +
  geom_bar(stat = "identity", position = "dodge") +
  labs(title = "ggplot2 - 分组条形图", 
       x = "类别", y = "数值") +
  scale_fill_manual(values = c("Value1" = "steelblue", "Value2" = "coral")) +
  theme_minimal()

出图效果如下:

  • 堆叠条形图
go 复制代码
ggplot(data_long, aes(x = Category, y = value, fill = variable)) +
  geom_bar(stat = "identity", position = "stack") +
  labs(title = "ggplot2 - 堆叠条形图", 
       x = "类别", y = "数值") +
  scale_fill_manual(values = c("Value1" = "steelblue", "Value2" = "coral")) +
  theme_minimal()

出图效果如下:

  • 百分比条形图
go 复制代码
ggplot(data_long, aes(x = Category, y = value, fill = variable)) +
  geom_bar(stat = "identity", position = "fill") +
  labs(title = "ggplot2 - 百分比堆叠条形图", 
       x = "类别", y = "百分比") +
  scale_y_continuous(labels = scales::percent) +
  scale_fill_manual(values = c("Value1" = "steelblue", "Value2" = "coral")) +
  theme_minimal()

出图效果如下:

4.高级条形图绘制

  • 误差条形图
go 复制代码
# 创建包含误差的数据
data_error <- data %>%
  mutate(se = Value1 * 0.1)  # 假设标准误为10%

ggplot(data_error, aes(x = Category, y = Value1)) +
  geom_bar(stat = "identity", fill = "lightblue", alpha = 0.7) +
  geom_errorbar(aes(ymin = Value1 - se, ymax = Value1 + se), 
                width = 0.2, color = "darkred") +
  labs(title = "带误差线的条形图", x = "类别", y = "数值") +
  theme_minimal()

出图效果如下:

  • 金字塔条形图(人口金字塔)
go 复制代码
# 创建示例数据
pyramid_data <- data.frame(
  AgeGroup = rep(c("0-10", "11-20", "21-30", "31-40", "41-50"), 2),
  Gender = rep(c("Male", "Female"), each = 5),
  Population = c(500, 600, 700, 650, 550, 480, 620, 720, 630, 520)
)

ggplot(pyramid_data, aes(x = AgeGroup, y = ifelse(Gender == "Male", 
                                                  -Population, Population), 
                         fill = Gender)) +
  geom_bar(stat = "identity") +
  coord_flip() +
  scale_y_continuous(labels = abs, limits = max(pyramid_data$Population) * c(-1, 1)) +
  labs(title = "人口金字塔图", x = "年龄组", y = "人口数量") +
  scale_fill_manual(values = c("Male" = "steelblue", "Female" = "pink")) +
  theme_minimal()

出图效果如下:

  • 极坐标条形图(玫瑰图)
go 复制代码
ggplot(data, aes(x = Category, y = Value1, fill = Category)) +
  geom_bar(stat = "identity", width = 1) +
  coord_polar() +
  labs(title = "极坐标条形图(玫瑰图)") +
  theme_minimal() +
  theme(axis.text.x = element_blank())

出图效果如下:

※※大家在使用的时候遇到任何问题欢迎留言,您的支持是我创作的最大动力。※※

相关推荐
Scott9999HH16 小时前
【IIoT流量实战】蒸汽管道阀门全关却仍有流量?用 Python 实现涡街信号 FFT 频谱分析与温压全补偿积算网关,深度拆解靠谱的涡街流量计厂家硬核技术标准
开发语言·python
码智社17 小时前
AES加密原理详解及Java实现加解密实战
java·开发语言
AI云海17 小时前
python 列表、元组、集合和字典
开发语言·python
萧瑟余晖18 小时前
JDK 26 新特性详解
java·开发语言
马优晨19 小时前
Freemarker 完整讲解(后端 Java 模板引擎)
java·开发语言·freemarker·freemarker 完整讲解·freemarker模板引擎
人邮异步社区21 小时前
怎么把C语言学到精通?
c语言·开发语言
心平气和量大福大21 小时前
C#-WPF-控件-TextBox 数据绑定
开发语言·c#·wpf
ttwuai21 小时前
Cursor 生成 CRUD 后,Go 后台接口别只测 200:JWT、RBAC 和 tenant_id 怎么验
开发语言·后端·golang
এ慕ོ冬℘゜1 天前
前端基础:什么是时间戳?JS获取时间戳三种方法与实战用途
开发语言·前端·javascript
执明wa1 天前
LayoutInflater详解: XML是如何变成View的?
android·xml·开发语言·android studio