Try   HackMD

ggplot2推薦圖形配置

LHB阿好伯, 2020/12/17

tags: R ggplot2 可視化

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

ggplot2繪製的圖形非常漂亮
但若是要應用在正式一點的報告中還是會建議進行微調
今天就來分享幾個我喜歡的配置
純屬個人喜好沒有絕對的優劣

首先先安裝與載入這次會介紹的套件

#install.packages("ggsci") #安裝套件 #install.packages("ggeasy") #install.packages("extrafont") library(ggplot2) library(ggsci) # 修改配色 library(ggeasy) # 修改字體配置

theme主題設定

ggplot2預設是淡灰色背景
ggplot2內建theme_XXX系列函數可以用於修改主題
默認theme_gray()

p1 <- ggplot(data = diamonds, mapping = aes(x = price, y = carat, colour = clarity)) + #設定資料集與映射資料 geom_point() p1

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

可用主題theme_XXX()

主題名稱 特點
theme_gray 帶有灰色背景和白色網格線的ggplot2主題
theme_bw 經典的ggplot2主題,對於使用投影機顯示的文件可能會更好呈現
theme_linedraw 僅在白色背景上具有各種寬度的黑色線條的主,具有類似的目的theme_bw。請注意,此主題有一些很細的線(<< 1 pt),有些期刊可能會拒絕。
theme_light 類似於theme_linedraw但具有淺灰色線條和軸的主題,用於將更多的注意力引向數據。
theme_dark theme_light的黑色表親,具有相似的線條,但背景較暗。有助於使彩色細線突出。
theme_minimal 沒有背景的簡約主題
theme_classic 具有x和y軸線且無網格線的經典外觀主題
theme_void 一個完全空的主題
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

可以添加theme_bw()或是theme_light()修改主題
theme_bw()或theme_light()個人覺得比較適合做為論文使用

p2 <- ggplot(data = diamonds, mapping = aes(x = price, y = carat, colour = clarity)) + #設定資料集與映射資料 geom_point() + theme_bw() #修改主題 p2

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

p2_1 <- ggplot(data = diamonds, mapping = aes(x = price, y = carat, colour = clarity)) + #設定資料集與映射資料 geom_point() + theme_light() #修改主題 p2_1

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

圖形配色

修改完主題再來介紹一個配色的套件 ggsci

ggsci提供了一組ggplot2受科學期刊,數據可視化,科幻電影和電視節目啟發的調色板

其中非常喜歡她其中的UCSCGB配色

使用方式也十分簡單只需要加上scale_color_ucscgb()scale_fill_ucscgb()

p3 <- ggplot(data = diamonds, mapping = aes(x = price, y = carat, colour = clarity)) + #設定資料集與映射資料 geom_point() + theme_bw() + scale_color_ucscgb() #使用ucscgb配色 p3

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

p4 <- ggplot(data = diamonds,mapping = aes(x = color)) + #設定資料集與映射資料 geom_bar(aes(group = cut,fill = cut)) + scale_fill_ucscgb() +#使用ucscgb填充配色 theme_bw() + ggtitle("p4") #增加圖片標題 p4

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

更改字體配置

更改字型 - extrafont

library(extrafont) #首次使用執行程式碼載入字體庫 #需等待五分鐘左右 font_import() #自訂字體 #font_import("C:\\Users\\...\\AppData\\Local\\Microsoft\\Windows\\Fonts\\") loadfonts(device = "win") fonts() #查看安裝字體

若是無法安裝可以改用showtext套件

library(showtext) font_add(family = "AHB", regular = "C:\\Users\\gtgrt\\AppData\\Local\\Microsoft\\Windows\\Fonts\\AHB.ttf") showtext_auto()
p5 <- p4 + ggtitle("p5") + theme(text=element_text(family = "Times New Roman")) #修改字體 # 標楷體要改為family = "DFKai-SB" p5

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

匯入字體時發生 had status 139 : No FontName. Skipping.

在Ubuntu中匯入時發生錯誤後來發現是新版Rttf2pt1的錯誤
只需要重新安裝舊版即可

library(remotes) remotes::install_version("Rttf2pt1", version = "1.3.8") extrafont::font_import()

更改字體大小 - ggeasy

最後看到XY軸的字體有點小
這邊可以使用ggeasy的套件進行修改

p6 <- p5 + ggtitle("p6") + easy_x_axis_title_size(16) + easy_y_axis_title_size(16) p6

到這邊大致上就是我會對ggplot2圖片的修改
更類似於我會在Sigmaplot所畫的圖片

最後總結一個今天使用到的程式碼

#install.packages("ggsci") #配色套件 #install.packages("ggeasy") #快速修改文字設定 #install.packages("extrafont") #使用電腦字形 library(ggplot2) library(ggeasy) library(ggsci) library(extrafont) #font_import() #載入字形 #loadfonts(device = "win") p <- ggplot() p + scale_fill_ucscgb() +#使用ucscgb填充配色 #scale_color_ucscgb() theme_bw() + #更改主題 theme(text=element_text(family = "Times New Roman")) + #修改字體 #theme(text = element_text(family = "AHB"))+ # theme(text=element_text(family = "DFKai-SB")) #標楷體 easy_x_axis_title_size(16) + #更改XY軸字體大小 easy_y_axis_title_size(16)

更改R的預設語系

有時候會發現ggplot2時間的輸出會是使用中文
然而可以修改系統語系更改輸出

install.packages("COVID19") #載入套件 library(COVID19) library(ggplot2) library(dplyr) x1 <- covid19(raw = FALSE) head(x1) #全球州數據 x2 <- covid19(level = 2) x3 <- x1 %>% group_by(date) %>% summarise_at('deaths',sum) p1 <- ggplot(data = x3,mapping = aes(x = date, y = deaths)) + geom_point() p1

#R Code #中改英 Sys.setlocale("LC_TIME", "English") #英改中 Sys.setlocale(category = "LC_ALL", locale = "cht")

🌟全文可以至下方連結觀看或是補充

全文分享至

https://www.facebook.com/LHB0222/

https://www.instagram.com/ahb0222/

有疑問想討論的都歡迎於下方留言

喜歡的幫我分享給所有的朋友 \o/

有所錯誤歡迎指教

全部文章列表

R語言網路免費基礎資料與個人推薦書單