Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用日期时调整x轴直方图_R_Plot_Histogram - Fatal编程技术网

R 使用日期时调整x轴直方图

R 使用日期时调整x轴直方图,r,plot,histogram,R,Plot,Histogram,我在变量x中存储了以下格式的数据集“%d%m%Y”。 我想用直方图绘制频率(每月) 日期从5月1日到10月31日 我使用了以下代码: hist(x,"months") 出现下图 您可以看到轴不是最佳的。它给人的印象是,4月份的数据是绘制出来的,而实际上是5月份的数据 有人能帮我把标签放在吧台中间吗?这意味着第一个元素是“五月”,它将位于酒吧的中间? 这里有一个例子 set.seed(1) x <- sample(seq(as.Date("2015-05-01"), as.Date("

我在变量x中存储了以下格式的数据集“%d%m%Y”。 我想用直方图绘制频率(每月)

日期从5月1日到10月31日

我使用了以下代码:

hist(x,"months")
出现下图

您可以看到轴不是最佳的。它给人的印象是,4月份的数据是绘制出来的,而实际上是5月份的数据

有人能帮我把标签放在吧台中间吗?这意味着第一个元素是“五月”,它将位于酒吧的中间?


这里有一个例子

set.seed(1)
x <- sample(seq(as.Date("2015-05-01"), as.Date("2015-10-31"), by="day"), 500, TRUE)
summary(x)
#         Min.      1st Qu.       Median         Mean      3rd Qu.         Max. 
# "2015-05-01" "2015-06-17" "2015-07-27" "2015-07-30" "2015-09-12" "2015-10-31" 

hist(x, "months")
set.seed(1)

x不确定为什么它不起作用。。。这是我的黑客工作

a <- hist(x, "months") # store the histogram data 
month <- strftime(x, "%m") # extract the month
dens <- (table(month)/sum(table(month)))/diff(a$breaks[-1]) # calculate the density
xax <- sort(c(as.numeric(unique(month)), max(as.numeric(unique(month))) + 1))
plot(xax, rep(max(dens), length(xax)), type = 'n', xaxt = 'n', yaxt = 'n', ylim = c(0, max(dens) + .002)) # blank plot
rect(xleft = xax[-length(xax)], ybottom = 0, xright = xax[-1], ytop = dens) # draw bins
axis(1, at = xax, labels =  months(seq(as.Date("2015-05-01"), as.Date("2015-11-30"), by="month"))) # add x axis
axis(2, at = seq(0, max(dens), length = 4), labels = format(seq(0, max(dens), length = 4), digits = 2)) # add y axis

a谢谢你的回答

这就是我在这段时间内如何处理它(代码不是最优的(干净的),因为它是为了显示解决步骤而编写的,而不是最终的代码):

#从日期格式中提取月份
x2
#Extract month from date format
x2 <- (format(x,"%m"))

#store it as a factor
x3 <- as.factor(x2)

#Replace factor names
levels(x3) <- c("May","June","July","August","September","October")
barplot(table(x3))
z <- as.factor((format(x,"%m")))

#Replacing factor names
levels(z) <- c("May","June","July","August","September","October")

#Plotting result
barplot(table(z))