From b16588985f3b6140ebb70eb3292eeede9c895448 Mon Sep 17 00:00:00 2001 From: ixxmu Date: Wed, 15 Jan 2025 07:59:32 +0000 Subject: [PATCH] A custom message for the commit --- ...40\207\350\275\264\346\210\252\346\226\255.md" | 15 --------------- 1 file changed, 15 deletions(-) delete mode 100644 "docs/2025-01/ggplot\347\224\273\345\233\276_\345\235\220\346\240\207\350\275\264\346\210\252\346\226\255.md" diff --git "a/docs/2025-01/ggplot\347\224\273\345\233\276_\345\235\220\346\240\207\350\275\264\346\210\252\346\226\255.md" "b/docs/2025-01/ggplot\347\224\273\345\233\276_\345\235\220\346\240\207\350\275\264\346\210\252\346\226\255.md" deleted file mode 100644 index b455c4c8..00000000 --- "a/docs/2025-01/ggplot\347\224\273\345\233\276_\345\235\220\346\240\207\350\275\264\346\210\252\346\226\255.md" +++ /dev/null @@ -1,15 +0,0 @@ ---- -title: "ggplot画图-坐标轴截断" -date: 2025-01-15T07:58:28Z -draft: ["false"] -tags: [ - "fetched", - "Hub of code" -] -categories: ["Acdemic"] ---- -ggplot画图-坐标轴截断 by Hub of code ------- -

在画图时,有时候纵坐标的数据相差太大。画出来的图比例不太协调,可以使用ggbreak这个包把纵坐标进行截断后,再在AI里面手动调整一下坐标轴会显得协调一些。

ggbreak对坐标轴进行截断scale_y_break

  • scale参考用于控制刻度线,大小
library(dplyr)
library(ggplot2)
library(ggbreak)

df <- data.frame(
x = c('a','b','c','d','e','f','g','h','i','j'),
y = c(rnorm(3) + 20, rnorm(3) + 10, rnorm(4) + 50) )

p1 <- ggplot(df,aes(x,y))+
geom_col(aes(fill=x))+
theme(legend.position = "none")+
scale_y_continuous(expand = c(0,0))
p1


p2<-p1+scale_y_break(c(30,40),#截断位置及范围
space = 0.3,#间距大小
scales = 1.5)#上下显示比例,大于1上面比例大,小于1下面比例大

p2
示例图


scale_y_break函数参数说明:

  • c(30,40)表示在坐标轴30-40区间进行截断
  • space表示截断后空白区域的宽度
  • scales截断后上下纵坐标长度在图片中所显示的相对比例


参考:

  1. https://zhuanlan.zhihu.com/p/388729095
  2. https://zhuanlan.zhihu.com/p/558355764
  3. https://cran.r-project.org/web/packages/ggbreak/vignettes/ggbreak.html


-
-原文链接