emehex emehex - 3 months ago 22
R Question

Shift geom_bar right (not center-aligned)

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

42- 42-
Answer

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.

Comments