Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Barplot ascending by group with partially rescaled y axis in R

I have a dataframe like the following:

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)

I need to do a barplot to show the number of obs for each name, with number of stud on top of each bar, and with bars arranged in ascending order by group.

Here is what I’ve done:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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")

and I got this:

enter image description here

Now I need to collapse the y axis only for values of obs above, let’s say, 500, in order to make more visible all the bars, even those that correspond to the smaller values of obs.

Thanks in advance.

>Solution :

Couple of options: try scale_y_log10 or ggbreak::scale_y_break

You’ll have to play around with the functions or other ggplot arguments for fine adjustments to get the appearance you need.

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)

Created on 2021-12-11 by the reprex package (v2.0.1)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading