Runic Runic - 1 year ago 166
R Question

How to change origin line position in ggplot bar graph?

Say I'm measuring 10 personality traits and I know the population baseline. I would like to create a chart for individual test-takers to show them their individual percentile ranking on each trait. Thus, the numbers go from 1 (percentile) to 99 (percentile). Given that a 50 is perfectly average, I'd like the graph to show bars going to the left or right from 50 as the origin line. In bar graphs in ggplot, it seems that the origin line defaults to 0. Is there a way to change the origin line to be at 50?

Here's some fake data and default graphing:

df <- data.frame(
names = LETTERS[1:10],
factor = round(rnorm(10, mean = 50, sd = 20), 1)


ggplot(data = df, aes(x=names, y=factor)) +
geom_bar(stat="identity") +

Answer Source

Picking up on @nongkrong's comment, here's some code that will do what I think you want while relabeling the ticks to match the original range and relabeling the axis to avoid showing the math:

ggplot(data = df, aes(x=names, y=factor - 50)) +
  geom_bar(stat="identity") +
  scale_y_continuous(breaks=seq(-50,50,10), labels=seq(0,100,10)) + ylab("Percentile") +
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download