• 主页
  • 课程

    关于课程

    • 课程归档
    • 成为一名讲师
    • 讲师信息
    教学以及管理操作教程

    教学以及管理操作教程

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

      关于课程

      • 课程归档
      • 成为一名讲师
      • 讲师信息
      教学以及管理操作教程

      教学以及管理操作教程

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

      老俊俊的生信笔记

      • 首页
      • 博客
      • 老俊俊的生信笔记
      • ggh4x 包的坐标轴调整技能!

      ggh4x 包的坐标轴调整技能!

      • 发布者 weinfoadmin
      • 分类 老俊俊的生信笔记
      • 日期 2021年11月23日
      测试开头


      人生就是一个笑话

      ggh4x 包的坐标轴调整技能!

      1引言

      ggh4x 除了对 ggplot 的 分面有强大的支持和扩展外 ,对于 ggplot 的 坐标轴调整 也有很多功能, 非常实用! 欢迎一起学习。

      ggh4x 包的坐标轴调整技能!

      2安装

      安装加载 R 包:

      install.packages('ggh4x')
      library(ggh4x)
      library(ggplot2)

      3position guides

      使用 guide_axis 和 guide_none 调整 xy 轴样式:

      g <- ggplot(mtcars, aes(wt, mpg)) +
        geom_point() +
        theme(axis.line = element_line(colour = "black"))

      g + guides(
        x = guide_none(title = "x"),
        x.sec = guide_axis(title = "x.sec"),
        y = "none",
        y.sec = "axis"
      )
      ggh4x 包的坐标轴调整技能!

      其它写法:

      g + scale_x_continuous(
        position = "bottom",
        guide = guide_axis(position = "top")
      )
      ggh4x 包的坐标轴调整技能!

      添加双坐标轴:

      g + scale_x_continuous(
        sec.axis = dup_axis(breaks = 2:4 + 0.5,
                            guide = guide_axis(angle = 45))
      )
      ggh4x 包的坐标轴调整技能!

      4Coloured axis

      给坐标轴添加颜色的不同方式,最后的更加简单:

      # Setting all theme elements is a mild inconvenience.
      g + theme(
        axis.line.x  = element_line(colour = "forestgreen"),
        axis.ticks.x = element_line(colour = "forestgreen"),
        axis.text.x  = element_text(colour = "forestgreen")
      )

      # A little bit easier
      g + guides(x = guide_axis_colour(colour = "forestgreen"))
      ggh4x 包的坐标轴调整技能!

      给第二条坐标轴改颜色:

      ggplot(economics, aes(date)) +
        geom_line(aes(y = unemploy)) +
        geom_line(aes(y = pop / 30), colour = "red") +
        scale_y_continuous(
          sec.axis = sec_axis(
            ~ .x * 30, name = "pop",
            guide = guide_axis_colour(colour = "red"))
        ) +
        theme(axis.line.y = element_line())
      ggh4x 包的坐标轴调整技能!

      5Truncated axes

      支持截断型坐标轴:

      g + guides(x = "axis_truncated")
      ggh4x 包的坐标轴调整技能!

      支持随意设置截断范围:

      # Using grid units to specify data-independent truncation points.
      g + guides(x = guide_axis_truncated(trunc_lower = unit(0.1, "npc"),
                                          trunc_upper = unit(0.9, "npc")))

      # Using atomic vectors are interpreted as data points that should be mapped.
      g + guides(x = guide_axis_truncated(trunc_lower = 2.5,
                                          trunc_upper = 4.5))
      ggh4x 包的坐标轴调整技能!

      使用隐式变量来传递,达到分段坐标轴样式:

      g + guides(x = guide_axis_truncated(trunc_lower = c(2, 4),
                                          trunc_upper = c(3, 5)),
                 y = guide_axis_truncated(trunc_lower = ~ .x - 1,
                                          trunc_upper = ~ .x + 1))
      ggh4x 包的坐标轴调整技能!

      还可以达到上下分段坐标轴的效果:

      df <- data.frame(x = seq(-3, 3, length.out = 6), y = LETTERS[1:6])

      ggplot(df, aes(x, y)) +
        geom_col() +
        scale_x_continuous(
          breaks = -3:0, guide = "axis_truncated",
          sec.axis = dup_axis(
            breaks = 0:3, guide = "axis_truncated"
          )
        ) +
        theme(axis.line.x = element_line())
      ggh4x 包的坐标轴调整技能!

      6Manual axes

      支持更自由的自定义坐标轴样式:

      g + guides(y.sec = guide_axis_manual(
        breaks = unit(c(1, 3), "cm"),
        labels = expression("treshold"^2, "annotation"[3]),
        label_colour = c("red", "blue"), label_size = c(8, 12)
      ))
      ggh4x 包的坐标轴调整技能!

      这样可以在第二坐标轴进行手动注释:

      tab <- table(diamonds$cut)

      ggplot(diamonds, aes(cut, price)) +
        geom_violin() +
        guides(x.sec = guide_axis_manual(
          breaks = names(tab),
          labels = paste0("n = ", tab)
        ))
      ggh4x 包的坐标轴调整技能!

      此外在绘制热图的时候,比如标注几个目的基因标签:

      highlight <- c("New York", "California", "Alabama", "Hawaii")

      clust <- hclust(dist(USArrests))

      # Melting USArrests
      df <- data.frame(
        state = rownames(USArrests)[as.vector(row(USArrests))],
        crime = colnames(USArrests)[as.vector(col(USArrests))],
        value = as.vector(as.matrix(USArrests)),
        row.names = NULL
      )

      ggplot(df, aes(crime, state, fill = value)) +
        geom_raster() +
        scale_y_dendrogram(hclust = clust, labels = NULL) +
        guides(y.sec = guide_axis_manual(breaks = highlight, labels = highlight))
      ggh4x 包的坐标轴调整技能!

      7Minor ticks

      给主刻度旁添加次要刻度线:

      g + guides(x = "axis_minor", y = "axis_minor")
      ggh4x 包的坐标轴调整技能!

      在指定范围添加次要刻度线:

      g + scale_x_continuous(
        minor_breaks = seq(2, 4, by = 0.2),
        guide = "axis_minor"
      )
      ggh4x 包的坐标轴调整技能!

      改变次要刻度线的长度:

      g + guides(x = "axis_minor") +
        theme(axis.ticks.length.x = unit(0.5, "cm"),
              ggh4x.axis.ticks.length.minor = rel(0.5))
      ggh4x 包的坐标轴调整技能!

      8Logarithmic ticks

      添加对数刻度,非常 nice!:

      pres <- ggplot(pressure, aes(temperature, pressure)) +
        geom_line()

      pres + scale_y_log10(guide = "axis_logticks") +
        theme(axis.ticks.length.y = unit(0.5, "cm"),
              ggh4x.axis.ticks.length.minor = rel(0.5),
              ggh4x.axis.ticks.length.mini = rel(0.2))
      ggh4x 包的坐标轴调整技能!

      下面方式不一样,达到的效果一样:

      # Using annotation log-ticks
      pres + scale_y_log10() +
        annotation_logticks(sides = 'l', outside = TRUE) +
        coord_cartesian(clip = "off") +
        theme(axis.text.y = element_text(margin = margin(r = 10)))

      # Using axis_logticks, setting tick length equivalently
      pres + scale_y_log10(guide = "axis_logticks") +
        theme(axis.ticks.length.y = unit(0.3, "cm"))
      ggh4x 包的坐标轴调整技能!

      把刻度朝里:

      pres + scale_y_log10(guide = "axis_logticks") +
        theme(axis.ticks.length.y = unit(-0.3, "cm"),
              axis.text.y = element_text(margin = margin(r = 10)))
      ggh4x 包的坐标轴调整技能!

      9Nested relations

      使用嵌套方式来给变量进行分类:

      df <- data.frame(
        item = c("Coffee", "Tea", "Apple", "Pear", "Car"),
        type = c("Drink", "Drink", "Fruit", "Fruit", ""),
        amount = c(5, 1, 2, 3, 1),
        stringsAsFactors = FALSE
      )

      ggplot(df, aes(interaction(item, type), amount)) +
        geom_col() +
        guides(x = "axis_nested")
      ggh4x 包的坐标轴调整技能!

      使用 paste 函数来匹配,用 nonsense 分割,然后注释:

      ggplot(df, aes(paste0(item, "~nonsense~", type), amount)) +
        geom_col() +
        guides(x = guide_axis_nested(delim = "nonsense"))
      ggh4x 包的坐标轴调整技能!

      weave_factors 函数可以保持顺序一一对应:

      ggplot(df, aes(weave_factors(item, type), amount)) +
        geom_col() +
        guides(x = "axis_nested")
      ggh4x 包的坐标轴调整技能!

      改变注释线条及文字样式:

      ggplot(df, aes(weave_factors(item, type), amount)) +
        geom_col() +
        guides(x = "axis_nested") +
        theme(
          axis.ticks = element_line(colour = "red"),
          ggh4x.axis.nestline.x = element_line(size = 2),
          ggh4x.axis.nesttext.x = element_text(colour = "blue")
        )
      ggh4x 包的坐标轴调整技能!

      可以展示多层嵌套关系:

      df$type2 <- c(rep("Consumables", 4), "Vehicle")
      df$appletea <- c("", rep("Ingredient of apple tea", 2), rep(NA, 2))

      ggplot(df, aes(weave_factors(item, type, appletea, type2), amount)) +
        geom_col() +
        guides(x = "axis_nested")
      ggh4x 包的坐标轴调整技能!

      10Dendrograms

      scale_y_dendrogram 函数可以给热图添加聚类树:

      clusters <- hclust(dist(USArrests), "ave")

      # reshaping USArrests
      df <- data.frame(
        State = rownames(USArrests)[row(USArrests)],
        variable = colnames(USArrests)[col(USArrests)],
        value = unname(do.call(c, USArrests))
      )

      g <- ggplot(df, aes(variable, State, fill = value)) +
        geom_raster()
      g + scale_y_dendrogram(hclust = clusters)
      ggh4x 包的坐标轴调整技能!

      使用对应的 y 主题调整参数可以调整树状图:

      g + scale_y_dendrogram(hclust = clusters) +
        theme(
        axis.ticks.y = element_line(size  = 2, lineend = "round"),
        axis.ticks.length.y = unit(10, "pt")
      )
      ggh4x 包的坐标轴调整技能!

      guide_dendro 函数可以调整位置:

      g + scale_y_dendrogram(guide = guide_dendro(position = "right"),
                             hclust = clusters)
      ggh4x 包的坐标轴调整技能!

      11梦想

      我有一个梦想, 所以只能做梦的时候想想, 但我需要梦想, 这样我才能在失败中站起来继续前进。

      ggh4x 包的坐标轴调整技能!


      ggh4x 包的坐标轴调整技能!


      欢迎加入生信交流群。加我微信我也拉你进 微信群聊 老俊俊生信交流群 哦,数据代码已上传至QQ群,欢迎加入下载。

      群二维码:

      ggh4x 包的坐标轴调整技能!

      老俊俊微信:


      ggh4x 包的坐标轴调整技能!

      知识星球:


      ggh4x 包的坐标轴调整技能!


      所以今天你学习了吗?

      欢迎小伙伴留言评论!

      点击我留言!

      今天的分享就到这里了,敬请期待下一篇!

      最后欢迎大家分享转发,您的点赞是对我的鼓励和肯定!

      如果觉得对您帮助很大,赏杯快乐水喝喝吧!



       往期回顾 




      ◀跟着 NC (Nature Comm) 学画图: 箱线图嵌套并分面添加文本注释

      ◀跟着 NC (Nature Communications) 学画图: 火山图进阶

      ◀跟着 Cell 学绘图: 优美的条形图

      ◀shinydashboard 让你制作一个酷炫的页面端

      ◀shiny 入门第四课: ui 详解

      ◀shiny 入门第三课: 进阶

      ◀寻找你心中的 TA !

      ◀shiny 入门第二课

      ◀shiny 入门第一课

      ◀DESeq2 归一化原理解析

      ◀…

      测试结尾

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

      • 分享:
      作者头像
      weinfoadmin

      上一篇文章

      跟着 NC (Nature Comm) 学画图: 箱线图嵌套并分面添加文本注释
      2021年11月23日

      下一篇文章

      shiny 之 滑块控件
      2021年11月24日

      你可能也喜欢

      8-1651542331
      跟着Nature学绘图(2) 箱线图-累积分布曲线图
      2 5月, 2022
      9-1651542322
      Julia 笔记之字符串
      2 5月, 2022
      0-1651542343
      Julia 笔记之数学运算和初等函数
      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年
      在线支付 激活码

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