Runic - 1 month ago 8

R Question

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)

)

library(ggplot2)

ggplot(data = df, aes(x=names, y=factor)) +

geom_bar(stat="identity") +

coord_flip()

Answer

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:

```
library(ggplot2)
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") +
coord_flip()
```

Source (Stackoverflow)

Comments