salomon
salomon

Reputation: 283

R ggplot with quantiles on the x-axis

I want to create a graph where the variable on the x-axis is highly right skewed. When plotting this type of distribution, it is difficult to see anything in the plot along most of the distribution.

To circumvent this problem, I am now trying to plot the quantiles on the x-axis rather than the concrete values, so that the values on the x-axis are evenly distributed in my plot.

As an MWE, I demonstrate the problem using the Boston data. The variable "crim" has a mean: 3.61; min: 0.00; Q25: 0.08; median: 0.26; Q75: 3.68; max: 88.98.

The corresponding plot looks as follows:

enter image description here

Any ideas on how to achieve this?

Code:

library(MASS)
library(ggplot2)
data(Boston)
dat <- Boston[, c("medv", "crim")]

ggplot(data = dat, aes(x = crim)) +
  geom_line(aes(y = medv)) +
  theme_bw()

Upvotes: 1

Views: 597

Answers (1)

teunbrand
teunbrand

Reputation: 38043

You can plot the rank of the variable. Divide that by the length of the variable and you effectively have quantiles.

library(MASS)
library(ggplot2)

ggplot(Boston, aes(x = rank(crim) / length(crim), y = medv)) +
  geom_line()

Created on 2021-05-09 by the reprex package (v0.3.0)

Upvotes: 3

Related Questions