• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Data Viz with Python and R

Learn to Make Plots in Python and R

  • Home
  • Python Viz
  • Seaborn
  • Altair
  • R Viz
  • ggplot2
  • About
    • Privacy Policy
  • Show Search
Hide Search

How To Avoid Overlapping Labels in ggplot2?

datavizpyr · March 11, 2020 ·

Dodge Overlapping X-axis Text with ggplot2 3.3.0
Avoid Overlapping Labels in ggplot2 3.3.0
A common problem in making plots, say a barplot or boxplot with a number of groups is that, names of the groups on x-axis label often overlap with each other. Till now, one of the solutions to avoid overlapping text x-axis is to swap x and y axis with coord_flip() and make a horizontal barplot or boxplot.

Now with the new version of ggplot2 2.3.0, one can easily dodge overlapping text on x-axis. ggplot2 version 2.3.0 has a function guide_axis() to help dodge and avoid overlapping texts on x-axis label.

Let us see a couple of examples of using guide_axis() to dodge and skip overlapping texts on x-axis. Let us first make sure the new ggplot2 version is installed.

# install ggplot2 from CRAN
install.packages("ggplot2")
# verify the ggplot2 version
packageVersion("ggplot2")

We will use gapminder data set to make plots in this example. Using gapminder data, we find countries with high mean life expectancy. We first group_by() country and get mean life expectancy for each country. And then select 10 countries randomly.

set.seed(42)
df <- gapminder %>%
  group_by(country) %>%
  summarize(mean_life =mean(lifeExp)) %>%
  sample_n(10) %>%
  mutate(country=fct_reorder(country, mean_life))

Now we have the data we need to make plots.

head(df)
## # A tibble: 6 x 2
##   country   mean_life
##   <fct>         <dbl>
## 1 Ghana          52.3
## 2 Italy          74.0
## 3 Lesotho        50.0
## 4 Swaziland      49.0
## 5 Zimbabwe       52.7
## 6 Thailand       62.2

Let us make a simple barplot with country on x-axis and mean life expectancy on y-axis.

df %>%
  ggplot(aes(x=country, y=mean_life))+
  geom_col()+
  labs(title="Overlapping X-axis Label Text in Barplot")

We can see that the country names on x-axis overlap with each other and we can not easily read them.

Overlapping X-axis Text in ggplot2
Overlapping X-axis Label Text in ggplot2

How to Avoid Overlapping Axis Text in ggplot2?

With the latest ggplot2 version 3.3.0, we have a fix for label overlap problem. We can use guide_axis() function to dodge overlapping axis text like country names on x-axis.

We will use guide_axis() within scale_x_discrete() as shown below. We have used the argument n.dodge=3 inside guide_axis().

df %>%
  ggplot(aes(x=country, y=mean_life))+
  geom_col()+
  scale_x_discrete(guide = guide_axis(n.dodge=3))+
  labs(title="Dodge Overlapping X-axis Label Text\nwith ggplot2 3.3.0")

Now we get a nice bar plot with no overlapping x-axis text. The argument n.dodge=3 arranges every three axis labels slightly away from x-axis. Depending on the length of label names we can change n.dodge.

Dodge Overlapping X-axis Text with guide_axis() in ggplot2 3.3.0
Dodge Overlapping X-axis Text with guide_axis() in ggplot2 3.3.0

How to Drop Some Overlapping Axis Text with ggplot2?

Sometimes you may want to skip some of the intermediate text on axis. We can drop overlapping axis text using guide_axis(check.overlap = TRUE) option.

df %>%
  ggplot(aes(x=country, y=mean_life))+
  geom_col()+
  scale_x_discrete(guide = guide_axis(check.overlap = TRUE))+
  labs(title="Drop Overlapping X-axis Label Text\nwith ggplot2 3.3.0")

We can see that guide_axis(check.overlap = TRUE) option has dropped overlapping x-axis text by keeping the first and last label.

Drop Overlapping X-axis Text with guide_axis() in ggplot2 3.3.0
Drop Overlapping X-axis Text with guide_axis() in ggplot2 3.3.0

Related posts:

Customizing Labels on Bars in Side by side Stacked BarplotHow To Add Labels to Grouped Barplot with Bars Side-By-Side in R? Customizing Legend Inside Scatter Plot ggplot2How To Place Legend Inside the Plot with ggplot2? Scree plot: barplot with geom_col()How To Make Scree Plot in R with ggplot2 Sinaplot and ViolinplotSinaplot vs Violin plot: Why Sinaplot is better than Violinplot

Filed Under: Dodge Overlapping Axis Text, ggplot2, ggplot2 version 2.3.0, R Tagged With: dodge labels R

Primary Sidebar

Tags

Altair barplot Boxplot boxplot python boxplot with jiitered text labels Bubble Plot Color Palette Countplot Density Plot Facet Plot gganimate ggplot2 ggplot2 Boxplot ggplot2 error ggplot boxplot ggridges ggtext element_markdown() Grouped Barplot R heatmap heatmaps Histogram Histograms Horizontal boxplot Python lollipop plot Maps Matplotlib Pandas patchwork pheatmap Pyhon Python R RColorBrewer reorder boxplot ggplot Ridgeline plot Scatter Plot Scatter Plot Altair Seaborn Seaborn Boxplot Stock Price Over Time Stripplot UpSetR Violinplot Violin Plot World Map ggplot2

Buy Me a Coffee

Copyright © 2025 · Daily Dish Pro on Genesis Framework · WordPress · Log in

Go to mobile version