案例
ggplot(head(age_data,10),aes(x=reorder(Country,age_median),y=age_median))+
geom_bar(aes(fill=Country),stat='identity')+
geom_text(aes(label=age_median),hjust=1.4,colour='white')+
coord_flip()+
theme_minimal()+
theme(legend.position='none')
相關知識1:畫出top n的統計圖形
在數據集上用head(xx,n)做限制就行。
相關知識2:要讓圖形(比如 條形圖)按一定順序排列。
在aes中的加上reorder函數,帶上對應的順序數據,如ase(x=reorder(XXX,XX),y=()). 其中XX為表順序的數據。
相關知識3:stat='identity'
stat參數表示對樣本點做統計的方式,默認為identity,表示一個x對應一個y,同時還可以是bin,表示一個x對應落到該x的樣本數。”說白了就是,identity提取橫坐標x對應的y值,bin提取橫坐標x的頻數
相關知識4:geom_text, 將文本直接添加到繪圖中
geom_text(aes(label=XXX),hjust=n,colour='xx'). XXX為加入的數據的數值,hjust表示傳入參數在圖中的位置,colour表示傳入數據的顏色。
相關資料:https://www.jianshu.com/p/cde77a937096
相關知識5:對坐標軸做倒置 coord_flip()
相關知識6:選擇圖形主題
theme_minimal() 為最簡主題,即去掉背景及其他,圖像看起來比較簡潔
補充ggplot2包資料: