查看原文
其他

分组、离断式坐标轴

阿越就是我 医学和生信笔记 2023-06-15
关注公众号,发送R语言Python,获取学习资料!


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

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)

library(ggprism)
library(ggplot2)
library(patchwork)

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

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

# 横坐标
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(040), 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()函数实现的。

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

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

g + guides(x = guide_axis_truncated(trunc_lower = c(24),
                                    trunc_upper = c(35)),
           
           # 注意看y轴的断线的长短
           y = guide_axis_truncated(trunc_lower = ~ .x - 1,
                                    trunc_upper = ~ .x + 2)
           )
plot of chunk unnamed-chunk-5

双坐标轴

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

# 双向图形分别添加坐标轴

df <- data.frame(x = seq(-33, 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

嵌套坐标轴

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

df <- data.frame(
  item = c("Coffee""Tea""Apple""Pear""Car"),
  type = c("Drink""Drink""Fruit""Fruit"""),
  amount = c(51231),
  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

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

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

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

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

ggplot(df, aes(weave_factors(item, type, appletea, type2), amount)) +
  geom_col() +
  guides(x = "axis_nested")
plot of chunk unnamed-chunk-9

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


以上就是今天的内容,希望对你有帮助哦!欢迎点赞、在看、关注、转发


欢迎扫描二维码加 QQ群 613637742


欢迎关注公众号:医学和生信笔记




往期回顾

zotero笔记界面大改造


一文搞懂临床预测模型的评价!


相关矩阵的ggplot2版本,方便拼图


tableone?table1?傻傻分不清楚


使用R语言快速绘制三线表


可能是最好用的R包安装教程


您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存