Breaking y-axis in ggplot2 with geom_bar

Almog Angel

I'm having a hard time dealing with this plot. The height of values in ANI>96 making it hard to read the red and blue percentage text. I failed to break the y-axis by looking at answers from other posts in StackOverflow.

Any suggestions?

Thanks.

library(data.table)
library(ggplot2)

dt <- data.table("ANI"= sort(c(seq(79,99),seq(79,99))), "n_pairs" = c(5, 55, 13, 4366, 6692, 59568, 382873, 397996, 1104955, 282915,
                 759579, 261170, 312989, 48423, 120574, 187685, 353819, 79468, 218039, 66314, 41826, 57668, 112960, 81652, 28613,
                 64656, 21939, 113656, 170578, 238967, 610234, 231853, 1412303, 5567, 4607268, 5, 14631942, 0, 17054678, 0, 3503846, 0),
                 "same/diff" = rep(c("yes","no"), 21))

for (i in 1:nrow(dt)) {
  if (i%%2==0) {
    next
  }
  total <- dt$n_pairs[i] + dt$n_pairs[i+1]
  dt$total[i] <- total
  dt$percent[i] <- paste0(round(dt$n_pairs[i]/total *100,2), "%")
  dt$total[i+1] <- total
  dt$percent[i+1] <- paste0(round(dt$n_pairs[i+1]/total *100,2), "%")
}

ggplot(data=dt, aes(x=ANI, y=n_pairs, fill=`same/diff`)) +
  geom_text(aes(label=percent), position=position_dodge(width=0.9), hjust=0.75, vjust=-0.25) +
  geom_bar(stat="identity") + scale_x_continuous(breaks = dt$ANI) +
  labs(x ="ANI", y = "Number of pairs", fill = "Share one common species taxonomy?") + 
  theme_classic() + theme(legend.position="bottom")
Edo

Here is the list of major changes I made:

  • I reduced the y axis by zooming into the chart with coord_cartesian (which is called by coord_flip).

  • coord_flip shouuld also improve the readability of the chart by switching x and y. I don't know if the switch is a desirable output for you.

  • Also now position_dodge, works as expected: two bars next to each other with the labels on top (on the left in this case).

  • I set geom_bar before geom_text so that the text is always in front of the bars in the chart.

  • I set scale_y_continuous to change the labels of the y axis (in the chart the x axis because of the switch) to improve the readability of the zeros.

ggplot(data=dt, aes(x = ANI, y = n_pairs, fill = `same/diff`)) +
    geom_bar(stat = "identity", position = position_dodge2(width = 1), width = 0.8) + 
    geom_text(aes(label = percent), position = position_dodge2(width = 1), hjust = 0, size = 3) +
    scale_x_continuous(breaks = dt$ANI) +
    scale_y_continuous(labels = scales::comma) +
    labs(x ="ANI", y = "Number of pairs", fill = "Share one common species taxonomy?") + 
    theme_classic() + 
    theme(legend.position = "bottom") +
    coord_flip(ylim = c(0, 2e6))

enter image description here


EDIT

Like this columns and labels are stacked but labels never overlap.

ggplot(data=dt, aes(x = ANI, y = n_pairs, fill = `same/diff`)) +
    geom_bar(stat = "identity", width = 0.8) + 
    geom_text(aes(label = percent,
                                hjust = ifelse(`same/diff` == "yes", 1, 0)), 
                        position = "stack", size = 3) +
    scale_x_continuous(breaks = dt$ANI) +
    scale_y_continuous(labels = scales::comma) +
    labs(x ="ANI", y = "Number of pairs", fill = "Share one common species taxonomy?") + 
    theme_classic() + 
    theme(legend.position = "bottom") +
    coord_flip(ylim = c(0, 2e6))

enter image description here

Alternatively, you can avoid labels overlapping with check_overlap = TRUE, but sometimes one of the labels will not be shown.

ggplot(data=dt, aes(x = ANI, y = n_pairs, fill = `same/diff`)) +
    geom_bar(stat = "identity", width = 0.8) + 
    geom_text(aes(label = percent), hjust = 1, position = "stack", size = 3, check_overlap = TRUE) +
    scale_x_continuous(breaks = dt$ANI) +
    scale_y_continuous(labels = scales::comma) +
    labs(x ="ANI", y = "Number of pairs", fill = "Share one common species taxonomy?") + 
    theme_classic() + 
    theme(legend.position = "bottom") +
    coord_flip(ylim = c(0, 2e6))

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

geom_bar + geom_line: with different y-axis scale?

geom_bar tied exactly to x and y axis (without aggregating)

Show percent of total on top of geom_bar in ggplot2 while showing counts on y axis

ggplot2 bar chart sign on y-axis

ggplot2: geom_bar with custom y limits

ggplot2 Bar Graph remove unnecessary distance between axis label and 0 count on y axis

How to color geom_bar by y-axis values?

ggplot2: Prevent geom_bar from being alphabetical, and y scale not showing breaks

How to get ggplot2 geom_bar to plot axis by it's factor levels and not by default sorting

Plot ratio of geom_bar on second y-axis

scaling x and y axis (geom_bar)

Why do geom_bar bars come from the y-axis and not the x-axis?

ggplot geom_bar with separate grouped variables on the x axis

ggplot2 bar chart gives weird y axis

How to create 5x1 matrix geom_bar facet and allowing negative value in bar to go down y-axis in ggplot?

Issues with ggplot2 geom_bar

Adjusting y axis origin for stacked geom_bar in ggplot2

How can I put labels on geom_bar() in ggplot2 (without a manual y_position)

How to arrange y axis in bar chart using ggplot2

Why is geom_bar y-axis unproportional to actual numbers?

Formatting Geom_Bar in ggplot2

Labelling geom_bar plot at a fixed distance from y-axis in ggplot

manipulate scale_y_log in geom_bar ggplot2

Changing the start point of X axis with ggplot2 and geom_bar

change decimal on y axis, geom_bar, "accuracy" not working

R ggplot2 geom_bar y-axis sequence similar to respective column values sequence

In ggplot geom_bar, the y axis labels are cluttered at the bottom of bar chart

R & ggplot2: 100% geom_bar + geom_line for average using secondary y axis

Tics on y axis in geom_bar not showing up