rggplot2bar-chartggpubr

Using ggbarplot to plot percentages with limits on percentage axis


I have a data frame like this in R:

df <- read.table(header = TRUE, text = "
group type Total Percentage
 'g1' 't1'   34.  70.588235
 'g1' 't2'   34.  26.470588
 'g1' 't3'   34.   2.941176
 'g2' 't1'   48.  41.666667
 'g2' 't4'   48.   6.25    
 'g2' 't2'   48.  47.916667
 'g2' 't3'   48.   4.166667
")

I want to create a stacked barplot using ggbarplot with the y-axis restricted from 0 to 100, but I get the following warning message:

library(ggpubr)
ggbarplot(df, x='group', y='Percentage', fill = 'type') + scale_y_continuous(limits = c(0, 100))
Warning message:
Removed 1 rows containing missing values (`geom_bar()`). 

Even after adding the expand parameter to scale_y_continuous call, one of the bar segments is not shown (t1 for group g2):

enter image description here

How can I create such a stacked barplot with the percentage axis (y-axis) limited from 0 to 100 but also showing all the filled in bars corresponding to the type?


Solution

  • I think you are looking for exapnd = c(0, 0) in scale_y_continuous(). Also, according to your edit, remove limits = c(0, 100).

    library(ggpubr)
    df |>
      ggbarplot(x = 'group', y = 'Percentage', fill = 'type') + 
      scale_y_continuous(expand = c(0, 0))