[英]How to put x-axis in order(Month) in R
I wanna plot with month,but the x-axis is not in order,such as"Apr","Aug","Nov"..... But I want the order on x-axis to be like "Jan", "Feb", "Mar"........ 我想绘制月份,但x轴的顺序不正确,例如“ Apr”,“ Aug”,“ Nov” .....但是我希望x轴的顺序像“ Jan”, “二月”,“三月” ........
#change the format of date
date_month <- format(date_1, "%b")
class(date_month)
[1] "character"
head(date_month)
[1] "Jul" "Jul" "Jul" "Jul" "Jul" "Jul"
plot(table(date_month), xlab = "Month", ylab = "count")
I tried this: 我尝试了这个:
x1 <- factor(date_month, levels=c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov","Dec"))
plot(y ~ x1)
and: 和:
plot(table(date_month), xlab = "Month", ylab = "count")
axis(date_month,labels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov","Dec"))
Doesn't work at all.Can someone help me with this?Many thanks. 根本不起作用,有人可以帮我吗?非常感谢。
in ur code used format to extract months but use base r function months
u will get solution easily 用您的代码使用格式提取月份,但使用基本r函数months
将轻松获得解决方案
If u use format
output is like : 如果您使用format
输出,例如:
> head(format(date_month$date, "%b"))
[1] "Jun" "Feb" "Mar" "Oct" "Oct" "Aug"
months
will extract the month name fully like below : months
将完全提取月份名称,如下所示:
> head(months(date_month$date))
[1] "June" "February" "March" "October" "October" "August"
as per ur code do the below : 根据您的代码执行以下操作:
date_month<-months(date_1)
date_month<-factor(date_month,levels=month.name)
Now plot and try. 现在绘图并尝试。
sample code : 样例代码:
date_month<-list(date=sample(seq(as.Date('2018/01/01'),
as.Date('2018/11/08'), by="day"), 100))
> head(date_month)
date
1 2018-06-13
2 2018-02-19
3 2018-03-05
4 2018-10-29
5 2018-10-25
6 2018-08-22
month.name
is a built-in 'constant' with all the months' names in long form. month.name
是内置的“常数”,所有月份的名称均采用长格式。 To match it to your data use substr()
and keep only first three characters. 要将其与您的数据匹配,请使用substr()
并仅保留前三个字符。
date_month <- factor(date_month, levels = substr(month.name, 1, 3))
Then plot it as usual. 然后照常绘制。
plot(table(date_month), xlab = "Month", ylab = "count")
Data 数据
set.seed(42)
date_1 <- as.Date(sample(0:364, 5e5, replace=TRUE),
origin="2018-01-01")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.