Vẽ bar chart nhưng kèm theo feeding level
Tiếp theo là đặt tên một vài lệnh
level_order=c("Protein","Fat","Energy","DM","Ash")
g=ggplot(Diet,aes(x = factor(Parameter,level_order),y=`g/kg`,fill=Treatment))
David=element_text(face = "bold", color = "black", size = 9)
David_lable=c(`High` = "High Feeding Level",`Low` = "Low Feeding Level")
Cuối cùng là vẽ biểu đồ
#Geom_bar (vẽ biểu đồ thanh, identity=y là giá trị g/kg trong excel, position=tách ra thành 4 cột nhở theo parameter)
g+geom_bar(stat = "identity",position=position_dodge(width=0.9))+
#Scale_fill_manual làm màu thủ công cho cột treatment
scale_fill_manual("Treatment",values=c("High_Pro" = "yellow", "High_Fat" = "Blue", "Mix" = "Pink","Initial"="Black","High_Carb"="burlywood1"))+
#Tên trục hoành và trục tung (bquote là cách ghi số lên hoặc xuống)
xlab("Parameter")+ylab(bquote(g.kg^-1))+
#Tên title,subtitle,caption
theme(plot.title = element_text(face = "bold",hjust = 0.5))+labs(title = "EFFECT OF MACRONUTRIENTS ON SNAKEHEAD GROWTH",subtitle = "BAR CHART FOR ILLUSTRATION",caption = "Data source: David La")+
# Màu sắc, kích thước các phân đoạn nhỏ trong trục tung và hoành
theme(axis.text.x=David)+theme(axis.text.y=David)+
# Chia trục tung
scale_y_continuous(breaks=c(0,50,100,150,200,250,300,350),limits=c(0,350))+
# Vị trí, màu sắc caption,title,subtitle
theme(plot.caption=element_text(hjust=0,face="italic",color="pink"),
plot.title=element_text(color="pink",hjust =0.5,size=14),
plot.subtitle=element_text(color="pink",size=11))+
#Facet_wrap = tên của 2 thanh chia nhóm,labeller=tên của facet wrap
facet_wrap(~`Feeding level`,labeller=as_labeller(David_lable))+
#Kích cỡ màu sắc background facet
theme(strip.text.x = element_text(size = 12,colour = "Red"))+
# Kích cỡ màu sắc chữ facet
theme(strip.background.x =element_rect(colour="Pink", fill="pink"))
