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
相关推荐
倔强的石头10630 分钟前
【C++指南】位运算知识详解
java·开发语言·c++
攻城狮7号33 分钟前
Python爬虫第19节-动态渲染页面抓取之Splash使用下篇
开发语言·爬虫·python·python爬虫
天天进步201538 分钟前
Python项目--基于计算机视觉的手势识别控制系统
开发语言·python·计算机视觉
mozun202043 分钟前
QT:Qt5 串口模块 (QSerialPort) 在 VS2015 中正确关闭串口避免被占用
开发语言·c++·qt·串口·串口调试·上位机软件
小刘同学++44 分钟前
Qt 中线程使用
开发语言·qt
徐寿春1 小时前
规则引擎 - Easy Rules
java·开发语言
byte轻骑兵1 小时前
【C++ 类和数据抽象】static 类成员
开发语言·c++
努力学习的小廉1 小时前
【C++】 —— 笔试刷题day_24
开发语言·c++
爱的叹息1 小时前
Java虚拟机(JVM)家族发展史及版本对比
java·开发语言·jvm
苏州向日葵2 小时前
C#学习知识点记录
开发语言·学习·c#