利用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())

出图效果如下:

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

相关推荐
2601_966949652 小时前
为什么量化策略需要大量历史股票数据?从回测可信度理解数据规模
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
niucloud-admin2 小时前
JAVA V6 多商户商城 开发文档——插件目录结构
java·开发语言
滕州市燕猫虎计算机科技工作室个体工商户2 小时前
Java锁
java·开发语言
多加点辣也没关系3 小时前
JavaScript|第31章:表单与控件
开发语言·javascript·ecmascript
菜鸟~noob2333 小时前
【电子战】 第09篇:阵列信号处理——波束形成、MVDR、MUSIC【含matlab代码】
开发语言·matlab·信号处理
ocean21033 小时前
2025-2026年Python面试高频知识点洞察
开发语言·python·面试·python八股文
零依赖极客3 小时前
Day 6·1 ARMv8.2 dotprod——vdotq_s32 一条指令做 4 个点积
c语言·开发语言·人工智能·矩阵·arm·vllm
新时代牛马5 小时前
cyclictest 毛刺从哪来?从ftrace、latencytop、perf到IRQ/调度延迟定位
android·开发语言·python·kotlin
三8445 小时前
Java 模板注入(FreeMarker) · 02 · 模板引擎与 FreeMarker 语法
java·开发语言·web安全·freemarker
IvanCodes6 小时前
Python 基础语法(七):推导式与常见遍历写法
开发语言·python