Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adjust the distance between x labels and the chart using ggplot2

Tags:

r

ggplot2

I am trying to cut the distance between x axis labels and the graph, so that it would be more clearly visible which bar responds to what lable. This is the code and the graph:

graph196 <- ggplot(serazene196a, aes(x = okres2, y = (NEPO_ANO_NE.mean/100), ordered=TRUE)) +
geom_bar(stat = "Identity", colour="white") 

graph196 + theme_stata() + theme(axis.text.x = element_text(angle = 90, hjust = 1, size = 10, vjust=0.5),
                             axis.text.y = element_text(angle = 0),
                             axis.title.x = element_blank(), axis.title.y = element_blank(),
             axis.text.y = element_text(size = 10), axis.ticks.x = element_blank(),
             axis.ticks.y = element_blank(),
             panel.border = element_blank(),
             plot.background = element_rect(fill = 'white')) +
scale_y_continuous(labels=percent) 

It looks like that: [1]: http://i.stack.imgur.com/bBjdn.jpg

If I increase hjust to 1.2, the labels appear close enough to the graph, but at the same time they are not aligned, so the graph does not look good at all: [1]: http://i.stack.imgur.com/C7Boc.jpg.

Is there an option of how to align the labels with increased hjust or otherwise bring the entire labels closer to the graph?

The problem is similar to this one: Adjust distance between x-axis and text in R plot, except the fact that I am using ggplot2.

like image 859
Jakub Drapal Avatar asked Jun 15 '16 16:06

Jakub Drapal


1 Answers

The problem is even though you've hidden tick marks they still occupy space. You can force the labels closer by adjusting the margins of your text labels.

element_text(angle = 90, hjust = 1, size = 10, vjust=0.5, margin=margin(-15,0,0,0)

The margins are defined as (top,right,bottom,left) and I've adjusted the top margin to be negative.

like image 76
darren17 Avatar answered Sep 21 '22 16:09

darren17