2021.4.26
持續(xù)更新中。。。
參考:《R數(shù)據(jù)可視化手冊》、學術(shù)數(shù)據(jù)分析及可視化
1. 簡單條形圖
BOD
str(BOD)
ggplot(BOD, aes(x=Time, y=demand)) +
#設(shè)置條形圖的形式、填充色、變寬色和寬帶
geom_bar(stat="identity", fill="lightblue",
colour="black", width=0.5)+
#設(shè)置標簽、垂直距離
geom_text(aes(label=demand), vjust=-0.2)
##x軸為離散型變量,X軸的變化
##ggplot(BOD, aes(x=factor(Time), y=demand)) + geom_bar(stat = "identity")
判斷x軸變量是連續(xù)性還是因子型很重要
條形圖中stat = 'identity'
不能少!
2. 單個條形圖添加誤差線
ce <- subset(cabbage_exp, Cultivar == "c39")
ggplot(ce, aes(x = Date, y = Weight))+
geom_bar(stat = "identity", fill = "white", color = "black")+
#添加誤差線,設(shè)置誤差線大小
geom_errorbar(aes(ymin=Weight-se, ymax=Weight+se),width=0.2)
3. 簇狀條形圖添加誤差線
ggplot(cabbage_exp, aes(x=Date, y=Weight, fill=Cultivar))+
#設(shè)置條形圖堆疊方式
geom_bar(stat="identity", position="dodge")+
geom_errorbar(aes(ymin=Weight-se,ymax=Weight+se),
position=position_dodge(0.9),width=0.2)
繪制簇狀條形圖時,需要用
position = "dodge"
來避免條形圖堆積
4. 堆積條形圖
#設(shè)置條形圖基本信息
ggplot(cabbage_exp, aes(x=Date, y=Weight, fill=Cultivar),
order=desc(Cultivar))+
geom_bar(stat="identity")
##調(diào)整圖例的順序
##guides(fill=guide_legend(revers=TRUE))
order = desc()
可以調(diào)整條形圖的堆疊順序
5. 繪制百分比堆積條形圖
library(plyr)
cabbage_exp
##將每組條形對應(yīng)的數(shù)據(jù)標準化為100%格式
ce <- ddply(cabbage_exp, "Date", transform,
percent_weight=Weight/sum(Weight)*100)
ggplot(ce, aes(x=Date, y=percent_weight, fill=Cultivar))+
geom_bar(stat="identity")