#分享 【ggplot2長條圖】台灣工業/服務業薪資大調查為例

megapx
今天看到一篇新聞在說台灣工業/服務業薪資的調查結果 裡面有兩張長條圖 今天就來試試看用ggplot2來畫畫看 ============目錄============ - x軸大小排序 - x軸修改角度 - 細調字體 - 修改y軸標籤增加輔助Y軸 - 增加資料標籤 - 修改特定資料呈現 - 全部文章列表 ============================= 資料來源:【赤裸裸的長條圖】台灣工業/服務業薪資大調查:半數員工年薪不到 50 萬元,這四大產業高於均值
==============R Code============== # 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 ==================================
megapx
🌟🌟🌟 x軸大小排序 首先我先依照使用sort函數進行排序 並稍微修改一下顏色 :::danger sort(x, index.return = TRUE, decreasing = TRUE) index.return = TRUE 回傳索引 decreasing = TRUE 降序排列 ::: ==============R Code============== order <- sort(plotdata$薪資中位數_1, index.return = TRUE, decreasing = TRUE)**** ================================== 後續使用factor函數將原本的向量資料改成因子 並且設定其levels控制其資料排列方式 ==============R Code============== 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 ==================================
megapx
## X軸修改角度 ==============R Code============== p3 <- p2 + theme(axis.text.x = element_text(angle = -45, hjust = 0.2, vjust = 0.5)) + ggtitle("p3") p3 ==================================
megapx
🌟 細調字體 使用在ggplot2推薦圖形配置中介紹的extrafont套件修改成我們想要的字體
==============R Code============== library(extrafont) windowsFonts(BL = windowsFont("微軟正黑體")) p4 <- p3 + theme(text=element_text(family = "BL"))+ #修改字體 ggtitle("p4") p4 ==================================
megapx
🌟🌟修改y軸標籤增加輔助Y軸 使用`scale_y_continuous`可以快速修改Y軸標籤生成輔助的Y軸 在ggplot2中的輔助軸主要是依靠主軸的大小去調整或增減 ==============R Code============== 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 ==================================
megapx
🌟🌟增加資料標籤 資料標籤有`geom_text``geom_label`兩種差別在於是否有外框 ==============R Code============== 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 ==================================
megapx
🌟🌟🌟修改特定資料呈現 ggplot2的優點就是他是一個圖層的概念 可以單一覆蓋原先的圖層 而不影響其他元素 ==============R Code============== 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 ==================================
megapx
🌟🌟🌟 依性別年紀與教育程度分類 這部分主要就是更改aes的資料 其餘複製前面的程式碼進行微調 利用之前快速進行ggplot2繪圖布置-patchwork文章中提到的套件進行合併即可
==============R Code============== 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) ==================================
megapx
🌟全文可以至下方連結觀看或是補充
全文分享至
有疑問想討論的都歡迎於下方留言 喜歡的幫我分享給所有的朋友 \o/ 有所錯誤歡迎指教 全部文章列表
megapx
愛心
56
留言
encourage first comment
有些話想說嗎 快分享出來彼此交流吧!