R语言ggplot2——柱形图

r 复制代码
BMI=read.table('/Users/zhangzhishuai/Downloads/33 lesson33 ggplot2散点图(一)/33_ggplot2/BMI.txt', header = T,row.names = 1,sep = '\t')
library(ggplot2)
BMI$name=rownames(BMI)
ggplot(BMI,aes(x=name,y=height)) +
  geom_bar(stat = 'identity' # identity:数值,stat:统计
           )

# 改变颜色
ggplot(BMI,aes(x=name,y=height)) +
  geom_bar(stat = 'identity',fill='lightblue',# 填充色
           color='blue' #边框颜色
           )

# 改变宽度
ggplot(BMI,aes(x=name,y=height)) +
  geom_bar(stat = 'identity',fill='lightblue',
           color='blue',
           width=0.3 # 改变宽度
  )
ggplot(BMI,aes(x=name,y=height, fill=gender)) +
  geom_bar(stat = 'identity',
           width=0.3 # 改变宽度
  )
ggplot(BMI,aes(x=name,y=height, fill=age # 渐变填充
               )) +
  geom_bar(stat = 'identity',
           width=0.3 # 改变宽度
  ) +
  scale_fill_gradient(low = 'yellow',high='red') #渐变填充

# 画出所有特征
library(reshape2)
bmi=melt(BMI[,-3],id='name')

# 堆积柱形图
ggplot(bmi,aes(x=name,y=value,fill=variable)) +
  geom_bar(stat = 'identity')

# 并排柱形图
ggplot(bmi,aes(x=name,y=value,fill=variable)) +
  geom_bar(stat = 'identity',position = 'dodge' # 并排放的意思
           )

# 添加文字
ggplot(BMI,aes(x=name,y=height)) +
  geom_bar(stat = 'identity',fill='lightblue') +
  geom_text(aes(label = height), # 标签
            vjust=-0.8, #垂直微调位置
            color='red') +
  ylim(0,190) # 设置纵轴高度

ggplot(bmi,aes(x=name,y=value,fill=variable)) +
  geom_bar(stat = 'identity',position = 'dodge') + 
  geom_text(aes(label = round(value,2)),# 保留两位小数
            hjust=-0.1, # 水平调
            vjust=0.4,
            position = position_dodge(0.9), # 调整排列
            angle = 90 # 角度
            ) + 
  ylim(0,200)

# 添加误差线
average=apply(BMI[,-c(3,6)],2 # 2指按照列处理
              ,function(x){tapply(x,BMI$gender,mean)})

average1=melt(average)
std = apply(BMI[,-c(3,6)],2 # 2指按照列处理
            ,function(x){tapply(x,BMI$gender,sd)})
std1=melt(std)

data = cbind(average1,std1$value)
names(data) = c('gender',"feature","mean",'std') #给data设置列名
ggplot(data,aes(x=gender,y=mean,fill=feature)) +
  geom_bar(stat = 'identity',position = 'dodge') + 
  geom_errorbar(aes(ymin=mean-std,ymax=mean+std),width=0.2,
                position = position_dodge(0.9)) +
  ylim(0,200)

names(data) = c('gender',"feature","mean",'std') #给data设置列名
ggplot(data,aes(x=feature,y=mean,fill=gender)) +
  geom_bar(stat = 'identity',position = 'dodge') + 
  geom_errorbar(aes(ymin=mean-std,ymax=mean+std),width=0.2,
                position = position_dodge(0.9)) +
  ylim(0,200)

# 只画某一个特征
subdata=subset(data,feature=='height')
ggplot(subdata,aes(x=feature,y=mean,fill=gender)) +
  geom_bar(stat = 'identity',position = 'dodge') + 
  geom_errorbar(aes(ymin=mean-std,ymax=mean+std),width=0.2,
                position = position_dodge(0.9)) +
  ylim(0,200)

# 画多个特征
subdata=subset(data,feature=='height' | feature=='weight')
ggplot(subdata,aes(x=feature,y=mean,fill=gender)) +
  geom_bar(stat = 'identity',position = 'dodge') + 
  geom_errorbar(aes(ymin=mean-std,ymax=mean+std),width=0.2,
                position = position_dodge(0.9)) +
  ylim(0,200) +
  guides(fill=FALSE) # 删除图注

BMI.txt

html 复制代码
name	height	weight	gender	BMI	age
tom	180	75	male	23.14814815	38
cindy	165	58	female	21.30394858	45
jimmy	175	72	male	23.51020408	43
sam	173	68	male	22.72043837	35
lucy	160	60	female	23.4375	32
lily	163	55	female	20.2020202	28
相关推荐
bst@微胖子24 分钟前
Python高级语法之selenium
开发语言·python·selenium
王小义笔记29 分钟前
Postman如何流畅使用DeepSeek
开发语言·测试工具·lua·postman·deepseek
java1234_小锋2 小时前
一周学会Flask3 Python Web开发-request请求对象与url传参
开发语言·python·flask·flask3
流星白龙5 小时前
【C++】36.C++IO流
开发语言·c++
诚信爱国敬业友善6 小时前
常见排序方法的总结归类
开发语言·python·算法
nbsaas-boot7 小时前
Go 自动升级依赖版本
开发语言·后端·golang
架构默片7 小时前
【JAVA工程师从0开始学AI】,第五步:Python类的“七十二变“——当Java的铠甲遇见Python的液态金属
java·开发语言·python
不只会拍照的程序猿8 小时前
从插入排序到希尔排序
java·开发语言·数据结构·算法·排序算法
小哥山水之间8 小时前
在 Python 中操作 Excel 文件
开发语言·python·excel
就爱学编程8 小时前
C语言预编译
c语言·开发语言