更改ggplot直方图的x轴标签 [英] Changing the x-axis labels of a ggplot histogram
本文介绍了更改ggplot直方图的x轴标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下数据集(为便于阅读而进行了编辑):
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()
对于一个特定的示例,我想更改x标签.
For a particular example I would like to change the x-labels however.
有什么想法可以解决这个问题吗?
Any thoughts on how I can pull this of?
推荐答案
为说明答案(并更好地理解问题),使用图片:
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()
产量:
在 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).
这篇关于更改ggplot直方图的x轴标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文