ggplot2堆積柱形圖筆記

#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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 文|陳小肥 第一次看到“喪偶式育兒”這個(gè)詞,是在朋友圈看到一個(gè)視頻,視頻開頭是外國爸爸獨(dú)自帶著兩個(gè)孩子,抱著年紀(jì)大...
    陳小肥卡閱讀 2,526評(píng)論 0 1
  • 今天中午游泳很在狀態(tài),渾身有使不完的勁,越游狀態(tài)越好。每每使勁蹬一下腿,向前劃一下都很爽的感覺,而且感覺...
    記得祝福閱讀 262評(píng)論 2 4
  • Leaked IAAF doping files: Wada 'very alarmed' by allegati...
    Goston007閱讀 327評(píng)論 0 2
  • 畫風(fēng)不一樣啊 你可能會(huì)問,高達(dá)模型不是很好做么?從板子上剪下來,貼上貼紙,插在一起就可以了啊。 讓我們看看下面這張...
    曹建峰閱讀 1,361評(píng)論 0 1

友情鏈接更多精彩內(nèi)容