nzcoops
nzcoops

Reputation: 9380

ggplot geom_bar - 'rotate and flip'?

test <- data.frame(
    y=seq(18,41,1),
    x=24:1
)

ggplot(test, aes(y=y, x=x)) + geom_bar(stat="identity", aes(width=1)) + 
    opts( axis.text.x = theme_blank(), axis.ticks.x = theme_blank()) + 
    scale_x_continuous(breaks=NULL) + 
    coord_cartesian(ylim = c(17, 42))

enter image description here

As far as the rotating and flipping is concerned, I'd like what is the y axis in this plot to be along the top, and what is the x axis to be down the right hand side. So the bars are coming 'out' of the right hand side of the plot, with the longest/tallest at the top, shortest at the bottom. If it's rotated clockwise 90 degrees, and then flipped over a vertical line that would achieve it.

coord_flip() and scale_y_reverse() get someway down the right path.

Edit:

I guess this is pretty close, just need to get that y axis to the top of the graph.

ggplot(test, aes(y=y, x=x)) + geom_bar(stat="identity", aes(width=1)) + 
    opts(axis.text.y = theme_blank(), axis.ticks.y = theme_blank()) + 
    scale_x_continuous(breaks=NULL) + scale_y_reverse() + coord_flip()  + scale_x_reverse()

Upvotes: 7

Views: 16423

Answers (1)

Andrie
Andrie

Reputation: 179398

Not exactly what you describe, but perhaps close enough?

ggplot(test, aes(y=y, x=x)) + geom_bar(stat="identity", aes(width=1)) +
  coord_flip() +
  xlim(24, 1) +
  ylim(42, 0)

The trick is to have the xlim and ylim arguments in reverse order. Your axis positions are still bottom and left...

enter image description here

Upvotes: 12

Related Questions