【发布时间】:2019-07-11 22:56:52
【问题描述】:
我有多个日期数据集,我想在 R 中使用 barplot 函数来绘制这些数据。这些数据是针对两个不同时期的,所以我想在 x 轴上显示其各自的日期以便于比较。到目前为止,这是我的代码。 A_Date 用于 A 中的数据集,而 B_Date 用于 B 中包含的数据集。
A= runif(24, min = 25, max = 45)
B=runif(24, min = 35, max = 100)
DF=rbind(A,B)
A_Date= as.data.frame(seq(as.Date("1987-01-01"), to= as.Date("1988-12-31"),by="months"))
names(A_Date)= "Dates"
A_Date$year=as.numeric(format(A_Date$Dates, "%Y"))
A_Date$month=as.numeric(format(A_Date$Dates, "%m"))
A_Date=A_Date[,-1]
A_Date = as.character(paste(month.abb[A_Date$month], A_Date$year, sep = "_" ))
B_Date= as.data.frame(seq(as.Date("2010-01-01"), to= as.Date("2011-12-31"),by="months"))
names(B_Date)= "Dates"
B_Date$year=as.numeric(format(B_Date$Dates, "%Y"))
B_Date$month=as.numeric(format(B_Date$Dates, "%m"))
B_Date=B_Date[,-1]
B_Date = as.character(paste(month.abb[B_Date$month], B_Date$year, sep = "_" ))
barplot(DF, beside = T, col = c("red","darkblue"), legend.text =c("1987-88", "2010-11"), args.legend =list(x="topleft", cex = 1.2, bty="n", x.intersp=0.2),
ylab = "Precipitation (mm)", cex.axis = 1.2, cex.lab=1.5)
另外,我想要 x 轴线(就像 y 轴上的线一样。 谢谢
【问题讨论】:
-
你想要一个base R的答案还是ggplot2?
标签: r date bar-chart axis axis-labels