Changing the x-axis labels of a ggplot histogram

15,194

To illustrate the answer (and better understand the question) a picture:

> require(ggplot2)
> chol <- read.table(url("http://assets.datacamp.com/blog_assets/chol.txt"), header = TRUE)
> ggplot(data=chol, aes(chol$AGE)) + geom_histogram()

yields:

The plot

There is the documentation (as we have a continuous not a discrete axis) at http://docs.ggplot2.org/current/scale_continuous.html

For a discrete axis one might have simply written:

> p <- ggplot(data=chol, aes(chol$AGE)) + geom_histogram() + scale_x_discrete(labels=c("20" = "twe", "30" = "thi", "40" = "fou", "50" = "fif", "60" = "six"))  # does NOT work cf. surrounding text.

A continuous axis at least allows formatting (cf. link for details).

Share:
15,194
Frits Verstraten
Author by

Frits Verstraten

Updated on June 14, 2022

Comments

  • Frits Verstraten
    Frits Verstraten almost 2 years

    I have the following dataset (edited for readability):

    chol <- read.table(url("http://assets.datacamp.com/blog_assets/chol.txt"), header = TRUE)
    

    And I am creating a histogram of the data doing:

    ggplot(data=chol, aes(chol$AGE)) + geom_histogram()
    

    For a particular example I would like to change the x-labels however.

    Any thoughts on how I can pull this of?