1

As explained in the title, when I am rotating the x label a gap appears between the x-axis and the labels. In addition, part of the labels are outside the drawing area. I tried several things, here is my reproducible code and a screenshot of the yielded plot. What I don't understand is that this gap is nonexistant when my labels are not rotated (angle=90)See screenshot 2.

library(ggplot2)
library(ggthemes)

df <- data.frame(country=c("Malaysia", "Mongolia", "Kazakhstan", "China", 
                           "Indonesia", "Philippines", "Turkmenistan",
                           "Maldives", "Pakistan", "Bhutan", "Thailand" ,"Myanmar", 
                           "India", "Bangladesh", "Afghanistan", "Nepal"),
                 Urbanization_Rate=c(72.8, 68.5, 53.6, 50.6, 50.7, 48.8, 
                                     48.7, 41.2, 36.2, 35.6, 34.1, 32.6, 31.3, 
                                     28.4, 23.5, 17))

ggplot(df, aes(x = reorder(country, -Urbanization_Rate),
               y = Urbanization_Rate)) + 
  geom_bar(stat = "identity", fill="darkseagreen3") +
  theme(axis.text.x=element_text(angle=60,vjust = 0.3, hjust=1,size=15)) + 
  theme(axis.text.y = element_text(size=13)) + 
  scale_y_continuous(expand = c(0, 0), limits = c(0, 80),
                     breaks=c(10,20, 30, 40,50,60,70,80)) +
  theme_hc() +
  theme(axis.title.x = element_blank())

enter image description here

enter image description here

ePoQ
  • 434
  • 3
  • 18

1 Answers1

1

You could adjust the vjust argument to 1 to have it aligned towards the xaxis again.

ggplot(df, aes(x = reorder(country, -Urbanization_Rate),
           y = Urbanization_Rate)) + 
  geom_bar(stat = "identity", fill="darkseagreen3") +
  theme(axis.text.x=element_text(angle=60,vjust = 1, hjust=1,size=15)) + 
  theme(axis.text.y = element_text(size=13)) + 
  scale_y_continuous(expand = c(0, 0), limits = c(0, 80),
                 breaks=c(10,20, 30, 40,50,60,70,80)) +
  theme_hc() + 
  theme(axis.title.x = element_blank())

enter image description here

Lennyy
  • 5,932
  • 2
  • 10
  • 23
  • Thank you. However, country names are no more aligned with the x label tickles but I am not gonna quibble over that :) – ePoQ May 30 '18 at 07:06
  • Yeah that's true, sorry. I could suggest you to read https://stackoverflow.com/questions/7263849/what-do-hjust-and-vjust-do-when-making-a-plot-using-ggplot for more explanation of what the hjust and vjust arguments do. If you want it to be aligned at the left, you could set hjust and vjust to 0 and adjust the margins around the plot by adding a line like this: + theme(plot.margin=unit(c(1,1,3,1.2),"cm")). However, then it seems like Malaysia belongs to the second bar, since then the last a is displayed just below the second bar. I hope this helps and you can try out multiple options now! – Lennyy May 30 '18 at 07:12