条形图按组上升,在 R 中部分重新缩放 y 轴

Barplot ascending by group with partially rescaled y axis in R

我有一个如下所示的数据框:

group <- c("A", "A", "B","B","B","C","B","A","B","B","C","B","B","C","B","B","B","B","B","C")
name <- c("nam1","nam2","nam3","nam4","nam5","nam6","nam7","nam8","nam9","nam10","nam11","nam12","nam13","nam14","nam15","nam16","nam17","nam18","nam19","nam20")
obs <- c(7500,950,450,160,150,220,110,60,40,40,40,15,15,15,10,10,10,10,10,10)
stud <- c(10,2,3,5,6,2,2,2,2,1,2,2,2,3,2,2,2,2,1,1)
df <- data.frame(group,name,obs,stud)

我需要做一个条形图来显示每个名字的 obs 数量,每个条顶部的螺柱数量,以及按组升序排列的条。

这是我所做的:

ggbarplot(df, x = "name", y = "obs",
          fill = "group",               
          color = "white",            
          palette = "jco",            
          x.text.angle = 90,           
          label = stud, label.pos="out", lab.size = 3, order = c("nam20","nam14","nam11","nam6","nam19","nam18","nam17","nam16","nam15","nam13","nam12","nam10","nam9","nam7","nam5","nam4","nam3","nam8","nam2","nam1"),
          xlab = "name",
          ylab = "obs")

我得到了这个:

现在我只需要为上面的 obs 值折叠 y 轴,比方说,500,以便使所有条形图更加可见,即使是那些对应于较小 obs 值的条形图。

提前致谢。

几个选项:尝试 scale_y_log10ggbreak::scale_y_break...

您必须尝试使用​​函数或其他 ggplot 参数进行微调以获得您需要的外观。

library(ggpubr)
library(ggplot2)
library(ggbreak)

ggbarplot(df, x = "name", y = "obs",
          fill = "group",               
          color = "white",            
          palette = "jco",            
          x.text.angle = 90,           
          label = stud, label.pos="out", lab.size = 3, order = c("nam20","nam14","nam11","nam6","nam19","nam18","nam17","nam16","nam15","nam13","nam12","nam10","nam9","nam7","nam5","nam4","nam3","nam8","nam2","nam1"),
          xlab = "name",
          ylab = "obs",
          title = "Log scale") + 
  scale_y_log10()


ggbarplot(df, x = "name", y = "obs",
          fill = "group",               
          color = "white",            
          palette = "jco",            
          x.text.angle = 90,           
          label = stud, label.pos="out", lab.size = 3, order = c("nam20","nam14","nam11","nam6","nam19","nam18","nam17","nam16","nam15","nam13","nam12","nam10","nam9","nam7","nam5","nam4","nam3","nam8","nam2","nam1"),
          xlab = "name",
          ylab = "obs",
          title = "ggbreak") +
  scale_y_continuous(limits = c(0, 7600))+
  scale_y_break(c(1000, 7200), scales = 0.3)

reprex package (v2.0.1)

于 2021-12-11 创建