Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.8k views
in Technique[技术] by (71.8m points)

r - Changing the x-axis labels of a ggplot histogram

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...