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
相关推荐
uppp»18 分钟前
深入理解 Java 反射机制:获取类信息与动态操作
java·开发语言
玩电脑的辣条哥2 小时前
Python如何播放本地音乐并在web页面播放
开发语言·前端·python
ll7788115 小时前
LeetCode每日精进:20.有效的括号
c语言·开发语言·算法·leetcode·职场和发展
Jackson@ML7 小时前
Python数据可视化简介
开发语言·python·数据可视化
赵琳琅7 小时前
Java语言的云计算
开发语言·后端·golang
lly2024067 小时前
jQuery 杂项方法
开发语言
赵琳琅7 小时前
MDX语言的安全开发
开发语言·后端·golang
开开又心心的学嵌入式7 小时前
C语言——指针进阶应用
c语言·开发语言
开开又心心的学嵌入式7 小时前
C语言——指针基础知识
c语言·开发语言
lonelyhiker7 小时前
javascript的原型链
开发语言·javascript·原型模式