R语言ggplot2——柱形图

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值