前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >分组、离断式坐标轴

分组、离断式坐标轴

作者头像
医学和生信笔记
发布2022-11-15 10:02:36
1.2K0
发布2022-11-15 10:02:36
举报
文章被收录于专栏:医学和生信笔记

ggplot2已经非常好用了,但是大家对美的追求是永无止境的,比如对于坐标轴,有人可能更喜欢base r那种,base r的很多默认图形,坐标轴都是分离的,比如这种:

代码语言:javascript
复制
barplot(c(20,30,40,50,60), names.arg = c(paste0('Col ',1:5)), col = "steelblue")

plot of chunk unnamed-chunk-1

ggplot2不是这样的,很多人看多了,又觉得还是默认图形好看,但是又苦于默认图形语法的难以理解和记忆。还有人想要分离的、成组的、截断的坐标轴等等。

很多扩展包都实现了,而且功能更加强大。

  • x轴和y轴分开/离断式坐标轴
    • ggprism实现
    • ggh4x实现
  • 双坐标轴
  • 嵌套坐标轴

x轴和y轴分开/离断式坐标轴

ggprism实现

先介绍基于ggprism的实现方式,这个包原本是用于模仿Graphpad Prism的图形风格的,非常好用,我前面专门介绍过,传送门:

让ggplot2变成Graphpad Prism样式:ggprism(01)

让ggplot2变成Graphpad Prism样式:ggprims(05)

代码语言:javascript
复制
library(ggprism)
library(ggplot2)
library(patchwork)

其中prism_offset可以实现x轴和y轴分开;

通过prism_bracket可以实现截断式的坐标轴,但是只能用于离散型变量。

代码语言:javascript
复制
# 横坐标
p1 <- ggplot(ToothGrowth, aes(x = factor(dose), y = len)) + 
  geom_jitter(aes(shape = factor(dose)), width = 0.2, size = 2) + 
  scale_shape_prism() + 
  theme_prism() + 
  theme(legend.position = "none") + 
  scale_y_continuous(limits = c(0, 40), guide = "prism_offset") # y轴和x轴分开

p2 <- p1 + scale_x_discrete(guide = "prism_bracket")

p1 + p2

plot of chunk unnamed-chunk-3

ggprism的实现方式比较简单,主要是模仿在graphpad prism中的样式。对于这类需要个性化坐标轴的操作,还是ggh4x更加擅长。

ggh4x实现

ggh4x是通过修改guide()函数实现的。

代码语言:javascript
复制
library(ggh4x)

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

g1 <- g + guides(x = "axis_truncated", y="axis_truncated")

g + g1

plot of chunk unnamed-chunk-4

可以自定义坐标轴截断的位置:

代码语言:javascript
复制
g + guides(x = guide_axis_truncated(trunc_lower = c(2, 4),
                                    trunc_upper = c(3, 5)),
           
           # 注意看y轴的断线的长短
           y = guide_axis_truncated(trunc_lower = ~ .x - 1,
                                    trunc_upper = ~ .x + 2)
           )

plot of chunk unnamed-chunk-5

双坐标轴

众所周知,ggplot2现在默认支持双坐标轴了,ggh4x为第2条坐标轴添加了更多自定义选项。

代码语言:javascript
复制
# 双向图形分别添加坐标轴

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",name = "second x")
  ) +
  theme(axis.line.x = element_line())

plot of chunk unnamed-chunk-6

嵌套坐标轴

对于有交互项的图形,也增加了更多的自定义选项。

代码语言:javascript
复制
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
)

# 默认情况
p1 <- ggplot(df, aes(interaction(item, type), amount)) +
  geom_col()

# 使用ggh4x修改
p2 <- ggplot(df, aes(interaction(item, type), amount)) +
  geom_col() +
  guides(x = "axis_nested")

p1 + p2

plot of chunk unnamed-chunk-7

对于这个嵌套坐标轴,可以进行非常多的细节修改,比如最常见的颜色、粗细等。

代码语言:javascript
复制
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 = 5),
    ggh4x.axis.nesttext.x = element_text(colour = "blue")
  )

plot of chunk unnamed-chunk-8

当然也是支持多重嵌套的。

代码语言:javascript
复制
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")

plot of chunk unnamed-chunk-9

就简单介绍到这里,其实还有很多细节可以修改,大家有兴趣可以自己探索,这个包很厉害,它扩展的很多细节修改可能不是那么优雅,但是确实解决了很多用户的痛点!

以上就是今天的内容,希望对你有帮助哦!

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-07-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 医学和生信笔记 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • x轴和y轴分开/离断式坐标轴
    • ggprism实现
      • ggh4x实现
      • 双坐标轴
      • 嵌套坐标轴
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档