阿好伯
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- disqus: ahb0222 GA : G-CQ4L16KHK4 --- # ggplot2推薦圖形配置 > [color=#40f1ef][name=LHB阿好伯, 2020/12/17][:earth_africa:](https://www.facebook.com/LHB0222/) ###### tags: `R` `ggplot2` `可視化` ![](https://i.imgur.com/9vwgKvv.jpg) [TOC] ggplot2繪製的圖形非常漂亮 但若是要應用在正式一點的報告中還是會建議進行微調 今天就來分享幾個我喜歡的配置 純屬個人喜好沒有絕對的優劣 首先先安裝與載入這次會介紹的套件 ```r= #install.packages("ggsci") #安裝套件 #install.packages("ggeasy") #install.packages("extrafont") library(ggplot2) library(ggsci) # 修改配色 library(ggeasy) # 修改字體配置 ``` ## theme主題設定 ggplot2預設是淡灰色背景 ggplot2內建theme_XXX系列函數可以用於修改主題 默認theme_gray() ```r= p1 <- ggplot(data = diamonds, mapping = aes(x = price, y = carat, colour = clarity)) + #設定資料集與映射資料 geom_point() p1 ``` ![](https://i.imgur.com/A3ZGhfz.png) ### 可用主題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|一個完全空的主題| ![](https://i.imgur.com/PEDaONT.gif) ![](https://i.imgur.com/EbMutRN.png) 可以添加theme_bw()或是theme_light()修改主題 theme_bw()或theme_light()個人覺得比較適合做為論文使用 ```r= p2 <- ggplot(data = diamonds, mapping = aes(x = price, y = carat, colour = clarity)) + #設定資料集與映射資料 geom_point() + theme_bw() #修改主題 p2 ``` ![](https://i.imgur.com/j7yfd8v.png) ```r= p2_1 <- ggplot(data = diamonds, mapping = aes(x = price, y = carat, colour = clarity)) + #設定資料集與映射資料 geom_point() + theme_light() #修改主題 p2_1 ``` ![](https://i.imgur.com/9Cv501l.png) ## 圖形配色 修改完主題再來介紹一個配色的套件 ==ggsci== ggsci提供了一組ggplot2受科學期刊,數據可視化,科幻電影和電視節目啟發的調色板 其中非常喜歡她其中的UCSCGB配色 使用方式也十分簡單只需要加上`scale_color_ucscgb()` 或`scale_fill_ucscgb()` ```r= p3 <- ggplot(data = diamonds, mapping = aes(x = price, y = carat, colour = clarity)) + #設定資料集與映射資料 geom_point() + theme_bw() + scale_color_ucscgb() #使用ucscgb配色 p3 ``` ![](https://i.imgur.com/lrRWhTJ.png) ```r= p4 <- ggplot(data = diamonds,mapping = aes(x = color)) + #設定資料集與映射資料 geom_bar(aes(group = cut,fill = cut)) + scale_fill_ucscgb() +#使用ucscgb填充配色 theme_bw() + ggtitle("p4") #增加圖片標題 p4 ``` ![](https://i.imgur.com/m6RGXHz.png) ## 更改字體配置 ### 更改字型 - extrafont ```r=+ library(extrafont) #首次使用執行程式碼載入字體庫 #需等待五分鐘左右 font_import() #自訂字體 #font_import("C:\\Users\\...\\AppData\\Local\\Microsoft\\Windows\\Fonts\\") loadfonts(device = "win") fonts() #查看安裝字體 ``` 若是無法安裝可以改用showtext套件 ```r= library(showtext) font_add(family = "AHB", regular = "C:\\Users\\gtgrt\\AppData\\Local\\Microsoft\\Windows\\Fonts\\AHB.ttf") showtext_auto() ``` ```r=+ p5 <- p4 + ggtitle("p5") + theme(text=element_text(family = "Times New Roman")) #修改字體 # 標楷體要改為family = "DFKai-SB" p5 ``` ![](https://i.imgur.com/qqwZemo.png) ### 匯入字體時發生 had status 139 : No FontName. Skipping. 在Ubuntu中匯入時發生錯誤後來發現是新版Rttf2pt1的錯誤 只需要重新安裝舊版即可 ```r= library(remotes) remotes::install_version("Rttf2pt1", version = "1.3.8") extrafont::font_import() ``` ## 更改字體大小 - ggeasy 最後看到XY軸的字體有點小 這邊可以使用ggeasy的套件進行修改 ```r=+ p6 <- p5 + ggtitle("p6") + easy_x_axis_title_size(16) + easy_y_axis_title_size(16) p6 ``` ![](https://i.imgur.com/jkJKY3b.png) 到這邊大致上就是我會對ggplot2圖片的修改 更類似於我會在Sigmaplot所畫的圖片 最後總結一個今天使用到的程式碼 ```r= #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時間的輸出會是使用中文 然而可以修改系統語系更改輸出 ```r= 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 ``` ![](https://i.imgur.com/Uicg45y.png) ```r= #R Code #中改英 Sys.setlocale("LC_TIME", "English") #英改中 Sys.setlocale(category = "LC_ALL", locale = "cht") ``` ![](https://i.imgur.com/2GHsflg.png) 🌟全文可以至下方連結觀看或是補充 全文分享至 https://www.facebook.com/LHB0222/ https://www.instagram.com/ahb0222/ 有疑問想討論的都歡迎於下方留言 喜歡的幫我分享給所有的朋友 \o/ 有所錯誤歡迎指教 # [:page_with_curl: 全部文章列表](https://hackmd.io/@LHB-0222/AllWritings) # [R語言網路免費基礎資料與個人推薦書單](/1TgzGGWGT_KLSvkDOwDmTw) ![](https://i.imgur.com/47HlvGH.png)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully