• 主页
  • 课程

    关于课程

    • 课程归档
    • 成为一名讲师
    • 讲师信息
    同等学历教学

    同等学历教学

    免费
    阅读更多
  • 特色
    • 展示
    • 关于我们
    • 问答
  • 事件
  • 个性化
  • 博客
  • 联系
  • 站点资源
    有任何问题吗?
    (00) 123 456 789
    weinfoadmin@weinformatics.cn
    注册登录
    恒诺新知
    • 主页
    • 课程

      关于课程

      • 课程归档
      • 成为一名讲师
      • 讲师信息
      同等学历教学

      同等学历教学

      免费
      阅读更多
    • 特色
      • 展示
      • 关于我们
      • 问答
    • 事件
    • 个性化
    • 博客
    • 联系
    • 站点资源

      R语言

      • 首页
      • 博客
      • R语言
      • R语言学习:多图排列组合,facet_matrix函数,RStudio与Github协同,tidyverse代码学习

      R语言学习:多图排列组合,facet_matrix函数,RStudio与Github协同,tidyverse代码学习

      • 发布者 weinfoadmin
      • 分类 R语言
      • 日期 2021年11月7日
      测试开头


      2021年第45周。


      这一周R语言学习,记录如下。


      01 

      多图排列组合


      patchwork包可以实现多图排列组合,并且功能强大、操作灵活。


      # 多图排列组合
      library(tidyverse)
      library(patchwork)
      theme_set(theme_light())

      dat <- palmerpenguins::penguins %>% filter(!is.na(sex))
      dat %>% View

      # 使用patchwork包进行多图排列组合
      # 第1个图
      point_plot <- dat %>% 
        ggplot(aes(bill_length_mm, flipper_length_mm, fill = sex)) +
        geom_jitter(size = 3, alpha = 0.5, shape = 21)
      point_plot

      # 第2个图
      point_plot2 <- dat %>% 
        ggplot(aes(bill_length_mm, bill_depth_mm, fill = sex)) +
        geom_jitter(size = 3, alpha = 0.5, shape = 21)
      point_plot2

      # 第3个图
      # plot_plot is obviously a fun name
      boxplot_plot <- dat %>% 
        ggplot(aes(x = body_mass_g, fill = sex)) +
        geom_boxplot()
      boxplot_plot

      # 上面3个图排列和组合
      # 使用patchwork包
      p <- (point_plot + point_plot2) / boxplot_plot
      p

      # 微调
      # 图例管理
      p + plot_layout(guides = "collect") 


      结果图

      R语言学习:多图排列组合,facet_matrix函数,RStudio与Github协同,tidyverse代码学习

      R语言学习:多图排列组合,facet_matrix函数,RStudio与Github协同,tidyverse代码学习


      02 

      使用ggforce包创建Subplots


      ggforce包的facet_matrix函数简易创建Subplots。


      library(ggforce)
      dat %>% 
        ggplot(aes(x = .panel_x, y = .panel_y, fill = sex)) +
        geom_point(alpha = 0.5, size = 2, shape = 21) +
        facet_matrix(
          vars(bill_length_mm, flipper_length_mm, bill_depth_mm, body_mass_g)
        )

      dat %>% 
        ggplot() +
        geom_boxplot(
          aes(
            x = .panel_x, 
            y = .panel_y, 
            fill = island, 
            group = interaction(.panel_x, island)
          )
        ) +
        facet_matrix(
          cols = vars(sex, species), 
          rows = vars(bill_depth_mm:body_mass_g)
        ) 


      结果图

      R语言学习:多图排列组合,facet_matrix函数,RStudio与Github协同,tidyverse代码学习


      R语言学习:多图排列组合,facet_matrix函数,RStudio与Github协同,tidyverse代码学习



      03 

      RStudio与Github协同


      RStudio与Github协同工作,进行代码管理。


      1 准备工作:

      1)安装R和RStudio

      2)安装Git

      3)注册Github账号

      如下图,我的Github账号:wangluqing

      R语言学习:多图排列组合,facet_matrix函数,RStudio与Github协同,tidyverse代码学习


      我的Github:

      https://github.com/wangluqing


      2 配置操作

      1)生成秘钥

      打开git bash,参考命令

      $ git config --global user.name "wanglq"
      $ git config --global user.email wangluqing360@163.com
      $ git config --global user.name
      $ git config --global user.email
      $ git config --list
      ssh-keygen -t rsa -C "wangluqing360@163.com"


      2)配置秘钥

      复制上面中生成的 id_rsa.pub 中的内容,之后登陆到 GitHub,在右上角的头像上点击 Settings – SSH and GPG keys,点击右边的 New SSH Key,然后将复制好的内容粘贴进去,标题自己随意取一个,比如 ds key,这样就完成了远程端的配置。


      3)测试连接

      在 git bash,输入如下命令

      ssh -T git@github.com

      结果

      R语言学习:多图排列组合,facet_matrix函数,RStudio与Github协同,tidyverse代码学习


      3 RStudio与Github协同

      1)登录Github,创建一个项目库,例如:Tidyverse_Study_Project

      2)打开项目库,复制ssh链接,如下图:

      R语言学习:多图排列组合,facet_matrix函数,RStudio与Github协同,tidyverse代码学习

      3)打开RStudio软件,选择使用版本控制创建项目,如下图:

      R语言学习:多图排列组合,facet_matrix函数,RStudio与Github协同,tidyverse代码学习


      R语言学习:多图排列组合,facet_matrix函数,RStudio与Github协同,tidyverse代码学习

      R语言学习:多图排列组合,facet_matrix函数,RStudio与Github协同,tidyverse代码学习


      项目创建成功后,就可以看到如下界面:

      R语言学习:多图排列组合,facet_matrix函数,RStudio与Github协同,tidyverse代码学习


      我的这个界面布局做了配置以及在项目下面创建了一些R脚本。


      4)项目建设和版本管理

      我们根据项目的任务和目标,在项目库下面创建一系列R脚本,每次创建完后,调试成功通过后,请同步到Github。

      打开Shell,执行如下命令就可以了

      git status
      git add .
      git commit -m 'tidyverse study'
      git push


      push成功后,就可以在Github对应项目库下查看相应代码了。


      我创建了R语言群,可以扫描下方二维码,备注:姓名-R语言,加我微信,进入R语言群,一起讨论。

      R语言学习:多图排列组合,facet_matrix函数,RStudio与Github协同,tidyverse代码学习



      04 

      tidyverse代码学习


      tidyverse包是我每天工作都要用到的R语言包。我喜欢代码学习法,即通过阅读、编写、修改、迁移代码等多种方式来学习知识和技能。


      tidyverse包学习的一份代码,总结和巩固常用函数的使用方法和实现功能。


      ####################
      # tidyverse学习代码片段
      ####################

      library(tidyverse)

      diamonds %>% 
        group_by(clarity) %>% 
        summarise(
          m = mean(price)
        ) %>% 
        ungroup()

      # ggplot包内置的数据集
      data(package = "ggplot2")
      glimpse(diamonds)
      str(diamonds)
      diamonds %>% 
        slice_head(n = 100) %>% 
        View
      ?diamonds

      # dplyr包
      # mutate()
      diamonds %>% 
        mutate(
          JustOne = 1,
          Values = "something",
          Simple = TRUE
        ) %>% 
        slice_tail(n = 100) %>% 
        View

      diamonds %>% 
        mutate(
          price200 = price - 200
        ) %>% 
        slice_head(n = 100) %>% 
        View

      diamonds %>% 
        mutate(
          price200 = price - 200,
          price20perc = price * 0.20,
          price20percoff = price * 0.80,
          pricepercarat = price / carat,
          pizza = depth ^ 2
        ) %>% 
        slice_sample(n = 10) %>% 
        View

      diamonds1 <- diamonds %>% 
        mutate(
          price200 = price - 200,
          price20perc = price * 0.20,
          price20percoff = price * 0.80,
          pricepercarat = price / carat,
          pizza = depth ^ 2
        ) %>% 
        slice_sample(n = 10)


      diamonds %>% 
        mutate(m = mean(price)) %>% 
        slice_head(n = 100) %>% 
        View

      diamonds %>% 
        mutate(
          m = mean(price),
          sd = sd(price),
          med = median(price)
        ) %>% 
        slice_head(n = 100) %>% 
        View

      # 使用recode函数对变量取值做重编码操作
      diamonds %>% 
        mutate(
          cut_new = recode(
            cut,
            "Fair" = "Okay",
            "Ideal" = "IDEAL"
          )
        ) %>% 
        slice_head(n = 100) %>% 
        View

      Sex <- factor(c("male", "m", "M", "Female", "Female", "Female"))
      TestScore <- c(10, 20, 10, 25, 12, 5)
      dataset <- tibble(Sex, TestScore)
      str(dataset)

      dataset %>% 
        mutate(Sex_new = recode(Sex, 
                                "m" = "Male",
                                "M" = "Male",
                                "male" = "Male"))

      # summarize()函数
      diamonds %>% 
        summarise(avg_price = mean(price))

      diamonds %>% 
        summarise(
          avg_price = mean(price),
          dbl_price = 2 * mean(price),
          random_add = 1 + 2,
          avg_carat = mean(carat),
          stdev_price = sd(price)
        ) %>% 
        slice_head(n = 100) %>% 
        View

      # group_by()函数和ungroup()函数
      ID <- c(1:50)
      Sex <- rep(c("male", "female"), 25)
      Age <- c(26, 25, 39, 37, 31, 34, 34, 30, 26, 33, 
               39, 28, 26, 29, 33, 22, 35, 23, 26, 36, 
               21, 20, 31, 21, 35, 39, 36, 22, 22, 25, 
               27, 30, 26, 34, 38, 39, 30, 29, 26, 25, 
               26, 36, 23, 21, 21, 39, 26, 26, 27, 21) 
      Score <- c(0.010, 0.418, 0.014, 0.090, 0.061, 0.328, 0.656, 0.002, 0.639, 0.173, 
                 0.076, 0.152, 0.467, 0.186, 0.520, 0.493, 0.388, 0.501, 0.800, 0.482, 
                 0.384, 0.046, 0.920, 0.865, 0.625, 0.035, 0.501, 0.851, 0.285, 0.752, 
                 0.686, 0.339, 0.710, 0.665, 0.214, 0.560, 0.287, 0.665, 0.630, 0.567, 
                 0.812, 0.637, 0.772, 0.905, 0.405, 0.363, 0.773, 0.410, 0.535, 0.449)

      data <- tibble(ID, Sex, Age, Score)
      data %>% 
        group_by(Sex) %>% 
        summarise(
          m = mean(Score),
          s = sd(Score),
          n = n()
        ) %>% 
        View

      data %>% 
        group_by(Sex) %>% 
        mutate(m = mean(Score)) %>%
        ungroup()

      # filter()函数
      diamonds %>% 
        filter(cut == "Fair") %>% 
        slice_head(n = 100) %>% 
        View

      diamonds %>%
        filter(cut %in% c("Fair", "Good"), price <= 600) %>% 
        slice_head(n = 100) %>% 
        View

      diamonds %>%
        filter(cut == "Fair", cut == "Good", price <= 600) %>% 
        slice_head(n = 100) %>% 
        View

      # select()函数
      diamonds %>% 
        select(cut, color) %>% 
        slice_head(n = 100) %>% 
        View

      diamonds %>% 
        select(-(1:5)) %>% 
        slice_head(n = 100) %>% 
        View

      diamonds %>% 
        select(x, y, z, everything()) %>% 
        slice_head(n = 100) %>% 
        View

      diamonds %>% 
        arrange(cut) %>% 
        slice_head(n = 100) %>% 
        View

      # count()函数
      diamonds %>% 
        count(cut)

      # 等价于
      diamonds %>% 
        group_by(cut) %>% 
        count(cut)

      # 等价于
      diamonds %>% 
        group_by(cut) %>% 
        summarise(n = n())


      # rename()函数
      diamonds %>% 
        rename(PRICE = price) %>% 
        slice_head(n = 100) %>% 
        View

      # 等价于
      diamonds %>% 
        mutate(
          PRICE = price
        ) %>% 
        select(- price) %>% 
        slice_head(n = 100) %>% 
        View

      # row_number()函数
      practice <- 
        tibble(Subject = rep(c(1,2,3),8),
               Date = c("2019-01-02", "2019-01-02", "2019-01-02", 
                        "2019-01-03", "2019-01-03", "2019-01-03",
                        "2019-01-04", "2019-01-04", "2019-01-04", 
                        "2019-01-05", "2019-01-05", "2019-01-05", 
                        "2019-01-06", "2019-01-06", "2019-01-06", 
                        "2019-01-07", "2019-01-07", "2019-01-07",
                        "2019-01-08", "2019-01-08", "2019-01-08",
                        "2019-01-01", "2019-01-01", "2019-01-01"),
               DV = c(sample(1:10, 24, replace = TRUE)),
               Inject = rep(c("Pos", "Neg", "Neg", "Neg", "Pos", "Pos"), 4))

      practice %>% 
        mutate(
          Session = row_number()
        ) %>% View

      practice %>% 
        group_by(Subject, Inject) %>%
        mutate(Session = row_number()) %>% 
        View

      # ifelse()函数
      practice %>% 
        mutate(Health = ifelse(Subject == 1,
                               "sick",
                               "healthy")) %>% 
        View


      每个代码片段具体可以做什么,你可以一边审核一边做运行与写解释,遇到问题可以来R语言群讨论。


      05

       探索性数据分析


      原始数据入手,采用数字化和可视化方式,对数据做探索性分析

      助于对数据的理解和认识

      基本思路:通过汇总统计和图形表示,以发现模式、异常和检验假设


      # R包
      library(tidyverse)
      library(DataExplorer)

      # 数据集
      dim(gss_cat)
      str(gss_cat)

      # 1) 数据集概览
      gss_cat %>% glimpse()

      # 2) 数据集简要
      gss_cat  %>% introduce()

      # 3) 数据简要信息可视化
      gss_cat %>% plot_intro()

      # 4) 变量缺失率分析
      gss_cat %>% plot_missing()
      gss_cat %>% profile_missing()

      # 5) 连续变量可视化
      gss_cat  %>% plot_density() # 密度曲线图
      gss_cat  %>% plot_histogram() # 直方图

      # 6) 类别变量可视化
      gss_cat  %>% plot_bar() # 条形图

      # 7) 相关性可视化图
      gss_cat  %>% plot_correlation()
      gss_cat  %>% plot_correlation(maxcat = 5) 

      # 8) 探索性分析报告
      gss_cat %>%
        create_report(
          output_file = "gss_survey_data_profile_report",
          output_dir = "./report/",
          y="rincome",
          report_title = "EDA Report"
        )


      部分结果图

      R语言学习:多图排列组合,facet_matrix函数,RStudio与Github协同,tidyverse代码学习


      R语言学习:多图排列组合,facet_matrix函数,RStudio与Github协同,tidyverse代码学习


      R语言学习:多图排列组合,facet_matrix函数,RStudio与Github协同,tidyverse代码学习

      (完整结果,请运行代码自测)



      06 

      制作优美的条形图


      条形图,商业分析或者报告中常用的一种图形表示。

      我们在做图的时候,要根据变量集的类型、个数、取值以及想通过数据表达什么信息等,综合考虑选择合适图形类型。

      优美的图形,就好比画画,是不断修改和打磨而成。


      library(tidyverse)
      election_data <- tribble(
        ~party, ~seats_won,
        "Australian Greens",          3,
        "Australian Labor Party",         55,
        "Liberal",         21,
        "The Nationals",          6,
        "Other Candidates",          3
      )

      election_data_sorted <- election_data %>%
        mutate(party = fct_reorder(party, seats_won, .desc = TRUE))

      ggplot(election_data_sorted,
             aes(x = seats_won, y = party, fill = party)) +
        geom_vline(xintercept = 44, linetype = 2, colour = "grey20") +
        geom_text(x = 45, y = 4, label = "majority ofnparliament", 
                  hjust = 0, size = 11 * 0.8 / .pt, colour = "grey20") +
        geom_col() +
        scale_x_continuous(expand = expansion(mult = c(0, 0.1))) +
        scale_y_discrete(limits = rev) +
        scale_fill_manual(breaks = c("Australian Labor Party", "Liberal", "The Nationals",
                                     "Australian Greens", "Other Candidates"),
                          values = c("#DE3533", "#0047AB", "#006644",
                                     "#10C25B", "#808080")) +
        labs(x = "Number of seats won",
             y = "Party",
             title = "Victorian election 2018 lower house results",
             caption = "Data source: Victorian Electoral Commission") +
        theme_bw() +
        theme(panel.grid.major.y = element_blank(),
              legend.position = "off")

      ggplot(election_data_sorted,
             aes(x = seats_won,
                 xend = 0,
                 y = party,
                 yend = party,
                 colour = party)) +
        geom_segment(size = 1.5) +
        geom_point(size = 3) +
        scale_x_continuous(expand = expansion(mult = c(0, 0.1))) +
        scale_y_discrete(limits = rev) +
        scale_colour_manual(breaks = c("Australian Labor Party", "Liberal", "The Nationals",
                                       "Australian Greens", "Other Candidates"),
                            values = c("#DE3533", "#0047AB", "#006644",
                                       "#10C25B", "#808080")) +
        labs(x = "Number of seats won",
             y = "Party",
             title = "Victorian election 2018 lower house results",
             caption = "Data source: Victorian Electoral Commission") +
        theme_bw() +
        theme(panel.grid.major.y = element_blank(),
              legend.position = "off")


      结果图:

      R语言学习:多图排列组合,facet_matrix函数,RStudio与Github协同,tidyverse代码学习





      我创建了R语言群,添加我的微信,备注:姓名-入群,我邀请你进群,一起学习R语言。


      R语言学习:多图排列组合,facet_matrix函数,RStudio与Github协同,tidyverse代码学习



      如果你想了解数据科学与人工智能,请关注下方公众号~

      如果你想找数据工作,请关注下方公众号~


      R语言学习专辑:

      2021年第44周:R语言学习

      2021年第43周:R语言学习

      2021年第42周:R语言学习

      2021年第41周:R语言学习

      2021年第40周:R语言学习

      2021年第39周:R语言学习

      2021年第38周:R语言学习

      2021年第37周:R语言学习

      2021年第36周:R语言学习

      2021年第35周:R语言学习

      2021年第34周:R语言学习



      觉得本文不错,就顺手帮我转发到朋友圈和微信群哦,谢谢。

      测试结尾

      请关注“恒诺新知”微信公众号,感谢“R语言“,”数据那些事儿“,”老俊俊的生信笔记“,”冷🈚️思“,“珞珈R”,“生信星球”的支持!

      • 分享:
      作者头像
      weinfoadmin

      上一篇文章

      跟着 Genome Research 学画图: 等高线散点图
      2021年11月7日

      下一篇文章

      掌握生信技术,玩转植物与土壤微生态领域的科研套路
      2021年11月7日

      你可能也喜欢

      3-1665801675
      R语言学习:重读《R数据科学(中文版)》书籍
      28 9月, 2022
      6-1652833487
      经典铁死亡,再出新思路
      16 5月, 2022
      1-1651501980
      R语言学习:阅读《R For Everyone 》(第二版)
      1 5月, 2022

      搜索

      分类

      • R语言
      • TCGA数据挖掘
      • 单细胞RNA-seq测序
      • 在线会议直播预告与回放
      • 数据分析那些事儿分类
      • 未分类
      • 生信星球
      • 老俊俊的生信笔记

      投稿培训

      免费

      alphafold2培训

      免费

      群晖配置培训

      免费

      最新博文

      Nature | 单细胞技术揭示衰老细胞与肌肉再生
      301月2023
      lncRNA和miRNA生信分析系列讲座免费视频课和课件资源包,干货满满
      301月2023
      如何快速批量修改 Git 提交记录中的用户信息
      261月2023
      logo-eduma-the-best-lms-wordpress-theme

      (00) 123 456 789

      weinfoadmin@weinformatics.cn

      恒诺新知

      • 关于我们
      • 博客
      • 联系
      • 成为一名讲师

      链接

      • 课程
      • 事件
      • 展示
      • 问答

      支持

      • 文档
      • 论坛
      • 语言包
      • 发行状态

      推荐

      • iHub汉语代码托管
      • iLAB耗材管理
      • WooCommerce
      • 丁香园论坛

      weinformatics 即 恒诺新知。ICP备案号:粤ICP备19129767号

      • 关于我们
      • 博客
      • 联系
      • 成为一名讲师

      要成为一名讲师吗?

      加入数以千计的演讲者获得100%课时费!

      现在开始

      用你的站点账户登录

      忘记密码?

      还不是会员? 现在注册

      注册新帐户

      已经拥有注册账户? 现在登录

      close
      会员购买 你还没有登录,请先登录
      • ¥99 VIP-1个月
      • ¥199 VIP-半年
      • ¥299 VIP-1年
      在线支付 激活码

      立即支付
      支付宝
      微信支付
      请使用 支付宝 或 微信 扫码支付
      登录
      注册|忘记密码?