rggplot2python-ggplot

How to avoid stacking names in the barplot?


I am making a barplot in ggplot2 where the "country" is compared to "suicides per 100,000 people". My problem is that country names are stacked on top of each other in the graph.

In the past, I tried with seaborn and matplotlib in python with the following code:

import seaborn as sns
import matplotlib.pyplot as plt
plt.figure(figsize=(20,50))
y = data['country']
sns.set_context("paper", 2.5, {"lines.linewidth": 4})
sns.countplot(y=y,label='count')

I highlight this because the variable 'country' was also implicit in the graph and it worked great, but now in R I can't find a similar solution.

barplot with seaborn and matplotlib

enter image description here

ggplot(country, aes(x = country, y = suicide_per_100k, fill = continent)) + 

geom_bar(stat = "identity") + 

geom_hline(yintercept = global_average, linetype = 2, color = "grey35", 
size = 1) +

labs(title = "Global suicides per 100k, by Country",
       x = "Country", 
       y = "Suicides per 100k", 
       fill = "Continent") +

coord_flip() +

scale_y_continuous(breaks = seq(0, 45, 2)) + 

theme(legend.position = "bottom")

I hope that the output has the names of the countries evenly distributed, like the following graphic:

enter image description here

But the actual output is as follows:

enter image description here


Solution

  • It looks like your country names are too cramped on their axis. Have you tried exporting your image with a larger height?

    I get an overlap problem when I export this graph at 1200 by 400: enter image description here

    But it looks better when I export it at 1200 by 700. enter image description here

    I hope this helps. If it does, it should be an easy fix. You might also try adjusting the size of the font on your y axis. Just add:

    theme(axis.text.y = element_text(size=12))
    

    With whatever font size you need.