Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shift geom_bar right (not center-aligned)

Tags:

r

ggplot2

I have a bar-chart that looks like this:

ggplot(mtcars, aes(x = cyl, y = hp)) + 
    geom_bar(stat = "identity", width = 1)

I want to tweak the geom_bar parameters so that the bars are shifted to the right (not centred)...

enter image description here

like image 337
emehex Avatar asked Aug 03 '16 20:08

emehex


1 Answers

Try this:

ggplot(mtcars, aes(x = cyl, y = hp)) + 
 geom_bar(stat = "identity", width = 1, position = position_nudge(x = 0.5))

How I figured this out: Went to ?geom_bar and saw that there was a postion argument and a link to ?position_dodge. Look at the examples there and made modifications to your code. Seems to be a satisfactory solution.

like image 72
IRTFM Avatar answered Oct 20 '22 00:10

IRTFM