R语言中ggplot2绘制柱状图
001、 基础绘图
type <- c('A', 'B', 'C', 'D', 'E', 'F', 'G') nums <- c(10,23,8,33,12,40,60) df <- data.frame(type = type, nums = nums) ## 生成的测试数据框 df ggplot(df, aes(type, weight = nums)) + geom_bar() ## 使用ggplot绘图
绘图结果如下:
002、给柱状图添加颜色
ggplot(df, aes(type, weight = nums, fill = type)) + geom_bar() ## 利用fill选项添加颜色
003、修改绘图输出的顺序
a、只修改原始数据顺序
type <- c('F', 'B', 'G', 'D', 'E', 'A', 'C') nums <- c(10,23,8,33,12,40,60) df <- data.frame(type = type, nums = nums) ## 测试数据保持不变,只修改顺序 ggplot(df, aes(type, weight = nums)) + geom_bar()
如下为绘图结果,可见绘图顺序并不是按照原始数据中的顺序输出,而是改变了顺序:
b、使修改原始数据后,绘图的输出顺序保持不变:
type <- c('F', 'B', 'G', 'D', 'E', 'A', 'C') nums <- c(10,23,8,33,12,40,60) df <- data.frame(type = type, nums = nums) ## 测试数据保持不变,只修改顺序 df$type <- factor(df$type, level = df$type) ggplot(df, aes(type, weight = nums)) + geom_bar()
004、手动指定输出柱状图的颜色
type <- c('A', 'B', 'C', 'D', 'E', 'F', 'G') nums <- c(10,23,8,33,12,40,60) df <- data.frame(type = type, nums = nums) col <- c("red", "blue", "green", "cyan", "purple", "black", ## 此处设定颜色 "magenta") ggplot(df, aes(type, weight = nums)) + geom_bar(fill = col) ## 绘制柱状图
绘图结果如下,可以看到颜色输出已经按照指定的颜色输出了:
005、根据判断语句设定颜色:
type <- c('A', 'B', 'C', 'D', 'E', 'F', 'G') nums <- c(10,23,8,33,12,40,60) df <- data.frame(type = type, nums = nums) ## 测试数据保持不变 ggplot(df, aes(type, weight = nums)) + geom_bar(fill = ifelse(df$nums > 20,'red','blue')) ## 利用判断语句输出颜色
绘图结果如下:
006、设置柱状图绘图的宽度
a、使用width参数设置为:0.3
type <- c('A', 'B', 'C', 'D', 'E', 'F', 'G') nums <- c(10,23,8,33,12,40,60) df <- data.frame(type = type, nums = nums) ## 测试数据保持不变 ggplot(df, aes(type, weight = nums)) + geom_bar(width = 0.3) ## 设置宽度width = 0.3
b、使用width参数设定为0.8:
type <- c('A', 'B', 'C', 'D', 'E', 'F', 'G') nums <- c(10,23,8,33,12,40,60) df <- data.frame(type = type, nums = nums) ## 测试数据保持不变 ggplot(df, aes(type, weight = nums)) + geom_bar(width = 0.8) ## 使用width参数设定宽度为0.8
007、手动指定x轴、y轴的名称
type <- c('A', 'B', 'C', 'D', 'E', 'F', 'G') nums <- c(10,23,8,33,12,40,60) df <- data.frame(type = type, nums = nums) ## 测试数据保持不变 ggplot(df, aes(type, weight = nums)) + geom_bar() + labs(x = 'x-name', y = 'y-name') ## 利用labs选项指定x轴、y轴的名称
008、手动修改x轴、y轴坐标轴名称的大小
a、设置坐标轴的大小:size = 10
type <- c('A', 'B', 'C', 'D', 'E', 'F', 'G') nums <- c(10,23,8,33,12,40,60) df <- data.frame(type = type, nums = nums) ## 测试数据保持不变 ggplot(df, aes(type, weight = nums)) + geom_bar() + theme(axis.title = element_text(size=10,face = "bold")) ## 利用该句修改坐标轴名称的大小
绘图结果如下:
b、设置坐标轴的大小:size = 30
type <- c('A', 'B', 'C', 'D', 'E', 'F', 'G') nums <- c(10,23,8,33,12,40,60) df <- data.frame(type = type, nums = nums) ## 测试数据保持不变 ggplot(df, aes(type, weight = nums)) + geom_bar() + theme(axis.title = element_text(size=30,face = "bold")) ## 设置绘图的大小为30
绘图的结果如下:
009、手动调整坐标轴上刻度标签的大小:
a、设施x轴刻度标签的大小,size = 10
type <- c('A', 'B', 'C', 'D', 'E', 'F', 'G') nums <- c(10,23,8,33,12,40,60) df <- data.frame(type = type, nums = nums) ## 测试数据保持不变 ggplot(df, aes(type, weight = nums)) + geom_bar() + theme(axis.text.x = element_text(size=10)) ## 此处调整坐标轴刻度标签名称的大小
b、设施x轴刻度标签的大小,size = 30
type <- c('A', 'B', 'C', 'D', 'E', 'F', 'G') nums <- c(10,23,8,33,12,40,60) df <- data.frame(type = type, nums = nums) ## 测试数据保持不变 ggplot(df, aes(type, weight = nums)) + geom_bar() + theme(axis.text.x = element_text(size=30)) ## 设置x轴刻度标签 seze = 30
绘图结果如下:
010、手动旋转坐标轴文字
a、x轴刻度标签旋转45度。
type <- c('A', 'B', 'C', 'D', 'E', 'F', 'G') nums <- c(10,23,8,33,12,40,60) df <- data.frame(type = type, nums = nums) ## 测试数据保持不变 ggplot(df, aes(type, weight = nums)) + geom_bar() + theme(axis.text.x = element_text(angle=45)) ## x轴旋转45度
b、x轴刻度标签旋转90度。
type <- c('A', 'B', 'C', 'D', 'E', 'F', 'G') nums <- c(10,23,8,33,12,40,60) df <- data.frame(type = type, nums = nums) ## 测试数据保持不变 ggplot(df, aes(type, weight = nums)) + geom_bar() + theme(axis.text.x = element_text(angle=90))
011、手动调整坐标轴刻度标签的位置
a、在x轴上面, 设置hjust为0.5
type <- c('A', 'B', 'C', 'D', 'E', 'F', 'G') nums <- c(10,23,8,33,12,40,60) df <- data.frame(type = type, nums = nums) ## 保持测试数据不变 ggplot(df, aes(type, weight = nums)) + geom_bar() + theme(axis.text.x = element_text( hjust = 0.5)) ## 此处hjust设置为0.5
结果如下图:
b、在x轴上面, 设置hjust为3.5
type <- c('A', 'B', 'C', 'D', 'E', 'F', 'G') nums <- c(10,23,8,33,12,40,60) df <- data.frame(type = type, nums = nums) ## 保持测试数据不变 ggplot(df, aes(type, weight = nums)) + geom_bar() + theme(axis.text.x = element_text( hjust = 3.5)) ## hjust设置为3.5
绘图结果如下:
c、在x轴上面, 设置hjust为-3.5
type <- c('A', 'B', 'C', 'D', 'E', 'F', 'G') nums <- c(10,23,8,33,12,40,60) df <- data.frame(type = type, nums = nums) ## 保持测试数据不变 ggplot(df, aes(type, weight = nums)) + geom_bar() + theme(axis.text.x = element_text( hjust = -3.5)) ## hjust设置为-3.5
绘图结果如下:
012、手动设置页边距
a、页边距设置为1
type <- c('A', 'B', 'C', 'D', 'E', 'F', 'G') nums <- c(10,23,8,33,12,40,60) df <- data.frame(type = type, nums = nums) ## 测试数据保持不变 ggplot(df, aes(type, weight = nums)) + geom_bar() + theme(axis.text.x = element_text( hjust = -3.5), plot.margin=unit(rep(1,4),'lines')) ## 设置页边距为1
b、页边距设置为4
type <- c('A', 'B', 'C', 'D', 'E', 'F', 'G') nums <- c(10,23,8,33,12,40,60) df <- data.frame(type = type, nums = nums) ## 保持测试数据不变 ggplot(df, aes(type, weight = nums)) + geom_bar() + theme(axis.text.x = element_text( hjust = -3.5), plot.margin=unit(rep(4,4),'lines')) ## 设置页边距为4
绘图结果如下:
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
2022-03-22 linux中将矩阵数据转换为一行数据
2022-03-22 linux中将矩阵数据转换为一列数据
2022-03-22 linux中如何将一列数据转换为一行数据
2021-03-22 c语言中数组中元素的个数