--- disqus: ahb0222 GA : G-VF9ZT413CG --- # 【ggplot2長條圖】台灣工業/服務業薪資大調查 > [color=#40f1ef][name=LHB阿好伯, 2020/02/09][:earth_africa:](https://www.facebook.com/LHB0222/) ###### tags: `可視化` `ggplot2` [TOC] [新聞:【赤裸裸的長條圖】台灣工業/服務業薪資大調查:半數員工年薪不到 50 萬元,這四大產業高於均值](https://buzzorange.com/techorange/2020/12/24/taiwan-2019-salary-survey-by-government/) | 產業別 | 薪資中位數_1 | 年增長率_1 | 性別 | 薪資中位數_2 | 年增長率_2 | 年齡 | 薪資中位數_3 | 年增長率_3 | 教育程度 | 薪資中位數_4 | 年增長率_4 | |---|---|---|---|---|---|---|---|---|---|---|---|---| | 電力及燃氣供應商 | 112.4 | -2.71 | 男 | 53 | 1.57 | ~25 | 34.9 | 3.64 | 國中以下 | 40.1 | 1.76 | | 金融及保險業 | 94.8 | 1.12 | 女 | 46.5 | 1.94 | 25~29 | 47.3 | 2.22 | 高中 | 44.3 | 1.52 | | 出版影音製作傳播及通訊業 | 70.4 | 24.8 | | | | 30~39 | 53.6 | 1.22 | 大專 | 53.9 | 1.43 | | 醫療保險業 | 64.5 | 2.05 | | | | 40~49 | 57.2 | 1.1 | 研究所 | 95.9 | 1.87 | | 運輸及倉儲業 | 60.3 | 3.24 | | | | 50~64 | 53.6 | 1.15 | | | | | 專業科學及技術服務業 | 58.8 | 6.19 | | | | 65~ | 40.4 | 1.36 | | | | | 礦業及土石採取業 | 56.7 | 3.75 | | | | | | | | | | | 製造業 | 50.5 | 1.01 | | | | | | | | | | | 不動產業 | 49.3 | 2.26 | | | | | | | | | | | 批發及零售業 | 48.7 | 2.57 | | | | | | | | | | | 用水供應及污染整治業 | 45.4 | 1.89 | | | | | | | | | | | 營建工程業 | 44.6 | 4.2 | | | | | | | | | | | 支援服務業 | 41.1 | 0.47 | | | | | | | | | | | 藝術娛樂及休閒服務業 | 37.9 | 2.79 | | | | | | | | | | | 住宿及餐飲業 | 36.4 | 3.39 | | | | | | | | | | | 其他服務業 | 34.8 | 1.33 | | | | | | | | | | | 教育業 | 26.6 | 4.27 | | | | | | | | | | ```r= # R Code library(readr) library(ggplot2) library(esquisse) datafile <- "C:\\R\\data.csv" plotdata <- read_csv(datafile, locale = locale()) p1 <- ggplot(data = plotdata, mapping = aes(x = 產業別, y = 薪資中位數_1))+ geom_bar(stat = "identity", width = 0.8) + ggtitle("P1") p1 ``` ![](https://i.imgur.com/GIUP8dW.png) # x軸大小排序 首先我先依照使用sort函數進行排序 並稍微修改一下顏色 :::danger sort(x, index.return = TRUE, decreasing = TRUE) index.return = TRUE 回傳索引 decreasing = TRUE 降序排列 ::: ```r= order <- sort(plotdata$薪資中位數_1, index.return = TRUE, decreasing = TRUE)**** ``` 後續使用factor函數將原本的向量資料改成因子 並且設定其levels控制其資料排列方式 ```r= order <- sort(plotdata$薪資中位數_1, index.return = TRUE, decreasing = TRUE) plotdata$產業別 <- factor(plotdata$產業別, levels = plotdata$產業別[order$ix]) p2 <- ggplot(data = plotdata, mapping = aes(x = 產業別, y = 薪資中位數_1)) + geom_bar(stat = "identity", width = 0.8, fill = 4)+theme_bw()+ ggtitle("P2") p2 ``` ![](https://i.imgur.com/oHM7aCx.png) ## X軸修改角度 ```r= p3 <- p2 + theme(axis.text.x = element_text(angle = -45, hjust = 0.2, vjust = 0.5)) + ggtitle("p3") p3 ``` ![](https://i.imgur.com/aaeHBCX.png) ### 細調字體 使用在[ggplot2推薦圖形配置](/L6Y0JBFwSHWl5RIk_Tjmsg)中介紹的extrafont套件修改成我們想要的字體 ```r= library(extrafont) windowsFonts(BL = windowsFont("微軟正黑體")) p4 <- p3 + theme(text=element_text(family = "BL"))+ #修改字體 ggtitle("p4") p4 ``` ![](https://i.imgur.com/9v5bVzi.png) ## 修改y軸標籤增加輔助Y軸 使用`scale_y_continuous`可以快速修改Y軸標籤生成輔助的Y軸 在ggplot2中的輔助軸主要是依靠主軸的大小去調整或增減 ```r= p5 <- p4 + geom_point(mapping = aes(x = 產業別, y = 年增長率_1*17),shape=2, color="red", size=3) + scale_y_continuous(name = expression("整年薪資中位數(萬)"), limits = c(0,125), sec.axis = sec_axis(~./17, name = "年增長率(%)"))+ ggtitle("P5") p5 ``` ![](https://i.imgur.com/wZFscQe.png) ## 增加資料標籤 資料標籤有`geom_text``geom_label`兩種差別在於是否有外框 ```r= p6 <- p5 + geom_text(mapping = aes(x = 產業別, y = 薪資中位數_1, label =薪資中位數_1),nudge_y = 5,size = 3)+ geom_label(mapping = aes(x = 產業別, y = 年增長率_1*17, label = paste0(年增長率_1,"%")),nudge_y = 6,size = 2.5)+ ggtitle("P6") p6 ``` ![](https://i.imgur.com/kuLMted.png) # 修改特定資料呈現 ggplot2的優點就是他是一個圖層的概念 可以單一覆蓋原先的圖層 而不影響其他元素 ```r= p7 <- p6 + geom_point(aes(1,0),shape=6, color="darkgreen", size = 1, stroke = 2.5)+ geom_label(mapping = aes(1,0, label = paste0(-2.71,"%")),nudge_y = 6,size = 2.5,color="darkgreen")+ geom_point(aes(6,6.19*17),shape=17, color="red", size = 3) + geom_label(mapping = aes(6, 6.19*17, label = paste0(6.19,"%")),nudge_y = 6, size = 2.5,colour = 2 ) + ggtitle("P7") p7 ``` ![](https://i.imgur.com/b3zZifn.png) # 依性別年紀與教育程度分類 這部分主要就是更改aes的資料 其餘複製前面的程式碼進行微調 利用之前[快速進行ggplot2繪圖布置-patchwork](/eSj1MyyJSNijH7feRJZesQ)文章中提到的套件進行合併即可 ```r= p9 <- ggplot(data = plotdata, mapping = aes(x = 性別, y = 薪資中位數_2)) + geom_bar(stat = "identity", width = 0.8, fill = 4, alpha = 0.7)+theme_bw() + geom_point(mapping = aes(x = 性別, y = 年增長率_2*17),shape=2, color="red", size=3) + scale_y_continuous(name = expression("整年薪資中位數(萬)"), limits = c(0,100), sec.axis = sec_axis(~./17, name = ""))+ scale_x_discrete(na.translate = FALSE)+ #刪除NA值 theme(axis.text.y.right =element_blank()) + #刪除輔助欄標籤 geom_text(mapping = aes( y = 薪資中位數_2, label =薪資中位數_2),nudge_y = 5,size = 3)+ geom_label(mapping = aes( y = 年增長率_2*17, label = paste0(年增長率_2,"%")),nudge_y = 6,size = 2.5)+ ggtitle("P9") p10 <- ggplot(data = plotdata, mapping = aes(x = 年齡, y = 薪資中位數_3))+ geom_bar(stat = "identity", width = 0.8, fill = 4, alpha = 0.7)+theme_bw() + geom_point(mapping = aes(x = 年齡, y = 年增長率_3*17),shape=2, color="red", size=3) + scale_y_continuous(name = expression(""), limits = c(0,100), sec.axis = sec_axis(~./17, name = ""))+ scale_x_discrete(na.translate = FALSE)+ #刪除NA值 theme(axis.text.y=element_blank()) + geom_text(mapping = aes( y = 薪資中位數_3, label =薪資中位數_3),nudge_y = 5,size = 3)+ geom_label(mapping = aes( y = 年增長率_3*17, label = paste0(年增長率_3,"%")),nudge_y = 6,size = 2.5)+ ggtitle("P10") order <- sort(plotdata$薪資中位數_4, index.return = TRUE, decreasing = FALSE) plotdata$教育程度 <- factor(plotdata$教育程度, levels = plotdata$教育程度[order$ix]) p11 <- ggplot(data = plotdata, mapping = aes(x = 教育程度, y = 薪資中位數_4))+ geom_bar(stat = "identity", width = 0.8, fill = 4, alpha = 0.7, na.rm = TRUE)+theme_bw() + geom_point(mapping = aes(x = 教育程度, y = 年增長率_4*17),shape=2, color="red", size=3) + scale_y_continuous(name = expression(""), limits = c(0,100), sec.axis = sec_axis(~./17, name = "年增長率(%)"))+ scale_x_discrete(na.translate = FALSE)+#刪除NA值 theme(axis.text.y.left =element_blank()) + geom_text(mapping = aes( y = 薪資中位數_4, label =薪資中位數_4),nudge_y = 5,size = 3)+ geom_label(mapping = aes( y = 年增長率_4*17, label = paste0(年增長率_4,"%")),nudge_y = 6,size = 2.5)+ ggtitle("P11") library(patchwork) design <- " 1223" p9+p10+p11 + plot_layout(design = design) ``` ![](https://i.imgur.com/gTDslIL.png) 🌟 🌟全文可以至下方連結觀看或是補充 全文分享至 https://www.facebook.com/LHB0222/ https://www.instagram.com/ahb0222/ 有疑問想討論的都歡迎於下方留言 喜歡的幫我分享給所有的朋友 \o/ 有所錯誤歡迎指教 # [:page_with_curl: 全部文章列表](https://hackmd.io/@LHB-0222/AllWritings) ![](https://i.imgur.com/47HlvGH.png)