旋转X标签时,X轴有间隙

时间:2018-05-30 06:41:29

标签: r plot ggplot2 bar-chart

如标题中所述,当我旋转x标签时,x轴和标签之间会出现间隙。此外,部分标签位于绘图区域之外。我尝试了几件事,这是我可重现的代码和产生的情节的截图。我不明白的是,当我的标签没有旋转时,这个差距是不存在的(angle=90)见截图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

1 个答案:

答案 0 :(得分:1)

您可以将vjust参数调整为1,使其再次与x轴对齐。

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