R语言制条形图,有数据版

最近因为工作上有r语言相关的制图安排,于是简单学习了一下R语言,先看条形图

怎么用R语言实现呢?

编译IDE:R Studio

library(dplyr)
library(ggplot2)
library(tidyr)
library(scales)
library(gridExtra)

# 读取数据,先手动把整个excel文件拆分成销售收入、地区、收入类型三种并保存为.csv文件
diqu_data <- read.csv('C:/Users/XX/Desktop/714picture/diqu.csv')
financial_data <- read.csv('C:/Users/XX/Desktop/714picture/financial data.csv')
fuben_data <- read.csv('C:/Users/XX/Desktop/714picture/fuben.csv')
#此处“C:/Users/XX/Desktop/714picture/”为文件所在位置,需要按照自己的文件修改

# 检查列名
print(colnames(fuben_data))
print(colnames(financial_data))
print(colnames(diqu_data))

# 处理 financial_data
colnames(financial_data) <- c("Year", "Total_Revenue")
financial_data$Year <- as.numeric(gsub(",", "", financial_data$Year))
financial_data$Total_Revenue <- as.numeric(gsub(",", "", financial_data$Total_Revenue))

# 绘制华为2006-2020销售收入图,添加数据标签和图框
p1 <- ggplot(financial_data, aes(x = Year, y = Total_Revenue, fill = Total_Revenue)) +
  geom_bar(stat = 'identity') +
  scale_fill_gradient(name = "销售收入", low = 'darkblue', high = 'lightblue') +
  scale_x_continuous(breaks = seq(2006, 2020, 1)) +
  geom_text(aes(label = Total_Revenue), vjust = -0.5, color = "black", size = 3) +
  labs(title = '华为2006-2020销售收入', x = '年份', y = '销售收入(百万万)') +
  theme(panel.border = element_rect(colour = "black", fill = NA, size = 1))

# 保存图1
ggsave("Huawei_2006_2020_Revenue.png", plot = p1, width = 10, height = 6)

# 处理 diqu_data
diqu_data_long <- diqu_data %>%
  pivot_longer(cols = starts_with("X"), names_to = "Year", names_prefix = "X", values_to = "Revenue") %>%
  mutate(Year = as.numeric(Year), Revenue = as.numeric(gsub(",", "", Revenue)))

# 数据准备:华为2006-2020销售收入占比(中国区、非中国区)
diqu_data_proportion <- diqu_data_long %>%
  filter(地区 %in% c("中国", "欧洲中东非洲", "亚太", "美洲")) %>%
  mutate(Region = ifelse(地区 == "中国", "China", "Non_China")) %>%
  group_by(Year, Region) %>%
  summarise(Revenue = sum(Revenue)) %>%
  mutate(Total = sum(Revenue, na.rm = TRUE)) %>%
  mutate(Proportion = Revenue / Total)

# 绘制华为2006-2020销售收入占比(中国区、非中国区)图
p2 <- ggplot(diqu_data_proportion, aes(x = Year, y = Proportion, fill = Region)) +
  geom_bar(stat = 'identity', position = 'fill') +
  scale_x_continuous(breaks = seq(2006, 2020, 1)) +
  labs(title = '华为2006-2020销售收入占比', x = '年份', y = '销售收入占比') +
  theme(panel.border = element_rect(colour = "black", fill = NA, size = 1))

# 保存图2
ggsave("Huawei_2006_2020_Revenue_Proportion_China.png", plot = p2, width = 10, height = 6)

# 处理 fuben_data
fuben_data_long <- fuben_data %>%
  pivot_longer(cols = starts_with("X"), names_to = "Year", names_prefix = "X", values_to = "Revenue") %>%
  mutate(Year = as.numeric(Year), Revenue = as.numeric(gsub(",", "", Revenue)))

# 数据准备:华为2006-2020销售收入占比(业务类型)
financial_data_business <- fuben_data_long %>%
  rename(Business_Type = 业务类型) %>%
  group_by(Year, Business_Type) %>%
  summarise(Revenue = sum(Revenue, na.rm = TRUE)) %>%
  mutate(Total = sum(Revenue, na.rm = TRUE)) %>%
  mutate(Proportion = Revenue / Total)

# 绘制华为2006-2020销售收入占比(业务类型)图,添加数据标签和图框
p3 <- ggplot(financial_data_business, aes(x = Year, y = Proportion, fill = Business_Type)) +
  geom_bar(stat = 'identity', position = 'fill') +
  scale_x_continuous(breaks = seq(2006, 2020, 1)) +
  labs(title = '华为2006-2020销售收入占比', x = '年份', y = '销售收入占比') +
  theme(panel.border = element_rect(colour = "black", fill = NA, size = 1))

# 保存图3
ggsave("Huawei_2006_2020_Revenue_Proportion_Business.png", plot = p3, width = 10, height = 6)

# 使用 gridExtra 包同时显示三张图并保存为 PNG 文件
combined_plot <- grid.arrange(p1, p2, p3, ncol = 3)
ggsave("Huawei_2006_2020_Combined.png", plot = combined_plot, width = 30, height = 10)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值