Changing the x-axis labels of a ggplot histogram

六月ゝ 毕业季﹏ 提交于 2021-02-05 08:12:22

问题


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?


回答1:


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:

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



来源:https://stackoverflow.com/questions/37589085/changing-the-x-axis-labels-of-a-ggplot-histogram

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!