#creat a dataset(生成數(shù)據(jù))
specie<-c(rep("sorgho",3),rep("poacee",3),rep("banana",3),rep("triticum",3))
condition<-rep(c("normal","stress","Nitrogen"),4)
value<-abs(round(rnorm(12,0,15),2))
df<-data.frame(specie,condition,value)
library(ggplot2)
#分組柱形圖
p1<-ggplot(df,aes(x=specie,y=value,fill=condition))+geom_bar(position="dodge",stat="identity")
p1+theme(legend.position = "none")#去掉右側(cè)圖例
p1+scale_fill_manual(values=c("blue","red","green"))#自定義填充色

Rplot09.png

Rplot10.png

Rplot11.png
#堆積柱形圖
ggplot(df,aes(x=specie,y=value,fill=condition))+geom_bar(stat="identity")
ggplot(df,aes(x=specie,y=value,fill=condition))+
geom_bar(stat="identity")+
geom_text(aes(label=value),position=position_stack(vjust=0.5))#添加標(biāo)簽
可以比較一下一下三條命令出圖的區(qū)別
ggplot(df,aes(x=specie,y=value,fill=condition))+
geom_bar(stat="identity")+
geom_text(aes(label=value))#1
ggplot(df,aes(x=specie,y=value,fill=condition))+
geom_bar(stat="identity")+
geom_text(aes(label=value),position=position_stack())#2 position_stack()參數(shù)用來調(diào)整添加的標(biāo)簽和每部分堆積柱子相匹配,默認(rèn)應(yīng)該是添加到每部分頂端
ggplot(df,aes(x=specie,y=value,fill=condition))+
geom_bar(stat="identity")+
geom_text(aes(label=value),position=position_stack(vjust=0.5))#3 vjust參數(shù)用來調(diào)整標(biāo)簽的為重,vjust=0.5將標(biāo)簽放到對(duì)應(yīng)部位的中部

Rplot12.png

Rplot13.png

Rplot14.png

Rplot15.png
基本的堆積柱形圖應(yīng)該做完了,接下來模仿這張圖片

QQ圖片20180712220000.jpg
暫時(shí)只想到一種解決辦法,更改數(shù)據(jù)格式,把之前用到的df數(shù)據(jù)集改成這樣:添加兩列用來指定文本和標(biāo)簽的位置
更改后的數(shù)據(jù)集
ggplot(df,aes(x=specie,y=value,fill=condition))+
geom_bar(stat="identity")+
geom_text(aes(label=specie,y=var4))+ylim(-20,65)+#設(shè)置縱坐標(biāo)范圍以掩蓋多余的標(biāo)簽
geom_label(aes(label=value,y=var5))+theme_bw()+
theme(axis.text.x = element_blank(),#去除橫坐標(biāo)軸標(biāo)簽
axis.ticks.x = element_blank(),#去除橫坐標(biāo)刻度
axis.line.x = element_blank(),
panel.border = element_blank(),#去掉邊框
axis.ticks.y = element_blank(),#去掉縱坐標(biāo)刻度
legend.position="none")#去掉圖例

效果基本滿意
PS:突然想到之前遇到的一個(gè)問題找到了解決辦法,明天(20180702)來補(bǔ)充;找資料還找到了python繪制堆積柱形圖的代碼,明天也重復(fù)一下。有些困了,睡覺
更新
之前學(xué)習(xí)過kaggle的一篇文章https://www.kaggle.com/apapiu/exploring-kobe-s-shots探索科比的投籃選擇
#完全重復(fù)其代碼
setwd("Rpractice/kaggle_practice_data/Kobe_shot_selection/")
df<-read.csv("data.csv",stringsAsFactors=F)
train<-df[!is.na(df$shot_made_flag),]
names(train)
train$shot_made_flag<-factor(train$shot_made_flag,levels=c("1","0"))#這句代碼很重要
ggplot(train,aes(x=season,fill=shot_made_flag))+geom_bar(stat="count",position="fill")+
scale_fill_manual(values=c("red","blue"))+
theme(axis.text.x = element_text(angle=90,vjust=0.5))#將橫坐標(biāo)標(biāo)簽調(diào)整為垂直,vjust 輕微調(diào)整位置
scale_fill_brewer(palette="Set1",direction=-1)#填充顏色

Rplot07.png
重復(fù)里面的堆積柱形圖時(shí)遇到的問題:如果我想讓上半部分紅色與下半部分藍(lán)色互換位置應(yīng)該怎么辦,答案:將代碼一換成代碼二
train$shot_made_flag<-factor(train$shot_made_flag,levels=c("1","0"))#代碼一
train$shot_made_flag<-factor(train$shot_made_flag,levels=c("0","1"))#代碼二

結(jié)果就變成了這樣
參考文章
https://stackoverflow.com/questions/34903368/how-to-center-stacked-percent-barchart-labels
https://stackoverflow.com/questions/45469147/ggplot2-update-stacked-barplot-with-percentage-labels
https://www.r-graph-gallery.com/48-grouped-barplot-with-ggplot2/
https://blog.csdn.net/Bone_ACE/article/details/47427453