R
ggplot
Published

July 28, 2024

Thousands categories

I wanted to create categories of thousands and needed to put numbers into intervals of one, ten, and hundred thousands as well as millions.

Here is the solution I came up with using \(10^{\lfloor \log_{10}(x) \rfloor}\)

Code
library(tidyverse)
options(scipen = 999)

set.seed(12)
n <- 12

dt <-
  tibble(seed = round(runif(n, 1, 10), 1)) |>
  mutate(
    x = seed * sample(10^(3:6), n, replace = TRUE),
    log_x = log10(x) |> round(1),
    floor_log_x = floor(log_x),
    category = 10^floor(log10(x))
  ) |>
  arrange(x)
Code
ggplot(dt, aes(y = factor(category))) +
  geom_bar(fill = "lightgreen") +
  labs(y = "category")

seed x log_x floor_log_x category
3.4 3400 3.5 3 1000
4.5 4500 3.7 3 1000
8.4 8400 3.9 3 1000
1.6 16000 4.2 4 10000
2.5 25000 4.4 4 10000
2.6 26000 4.4 4 10000
6.8 68000 4.8 4 10000
9.5 95000 5.0 5 10000
1.2 120000 5.1 5 100000
1.1 1100000 6.0 6 1000000
1.3 1300000 6.1 6 1000000
8.3 8300000 6.9 6 1000000
Code
pl <-
  ggplot(tibble(x = c(500, 20000000)), aes(x = x)) +
  stat_function(fun = log10, color = "lightblue", linewidth = 1.5) +
  geom_point(data = tibble(x = 10^(3:7)), aes(x = x, y = log10(x)), color = "blue", size = 3) +
  labs(x = "x", y = "log10(x)") +
  scale_x_log10(labels = scales::label_comma())

pl