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

你可能感兴趣的:(R语言学习,r语言,开发语言)