如何对星期几进行直方图并具有字符串标签

发布于 2024-11-27 18:54:17 字数 1344 浏览 1 评论 0原文

我有一个日期数据框(日期对象);见底部。 我试图将它们转换为星期几,然后绘制直方图,但理想情况下标签是“星期一”...“星期日”(不是数字)

我有两个不同的问题:

  1. 将 Date 对象转换为day-of-week,但结果是字符串或数字,而不是对象。
  2. 当我得到直方图时,垃圾箱和标签是错误的(见下文)。

如果我使用weekdays(dat),输出是字符串(“Monday”...),不能在hist()中使用。

或者,如果我转换为数字数据,如何在 hist() 上获取字符串标签?

> dotw <- with( month.day.year(dat[,1]), day.of.week(month,day,year) )
> hist(xxx,labels=c('M','Tu','W','Th','F','Sa','Su'),col='black') # WTF?!
> hist(dotw,xlab=list('M','Tu','W','Th','F','Sa','Su'))

无法按照标签的预期工作。 0.5 宽度的垃圾箱有什么用?另外,如何防止Sunday->0和Monday->1之间缺少间隙?理想情况下,列之间没有间隙。

我的数据如下所示:

> dat
  [1] "2010-04-02" "2010-04-06" "2010-04-09" "2010-04-10" "2010-04-14" "2010-04-15" "2010-04-19"
  [8] "2010-04-21" "2010-04-22" "2010-04-23" "2010-04-26" "2010-04-28" "2010-04-29" "2010-04-30"
 ...

> str(dat)
 Date[1:146], format: "2010-04-02" "2010-04-06" "2010-04-09" "2010-04-10" "2010-04-14" "2010-04-15" ...

> str(weekdays(dat))
 chr [1:146] "Friday" "Tuesday" "Friday" "Saturday" "Wednesday" "Thursday" "Monday" ...
> hist(weekdays(dat))
Error in hist.default(weekdays(dat)) : 'x' must be numeric

I have a data-frame of dates (Date object); see bottom.
I'm trying to convert them to day-of-week and then draw a histogram, but ideally where the labels are 'Monday'...'Sunday' (not numeric)

I have two distinct problems:

  1. It's easy to convert a Date object to day-of-week, but the result is string or numeric, not an object.
  2. When I get a histogram, the bins and labels are wrong (see below).

If I use weekdays(dat), the output is string ("Monday"...) which cannot be used in hist().

Alternatively, if I convert to numeric data, how to get string labels on hist()?

> dotw <- with( month.day.year(dat[,1]), day.of.week(month,day,year) )
> hist(xxx,labels=c('M','Tu','W','Th','F','Sa','Su'),col='black') # WTF?!
> hist(dotw,xlab=list('M','Tu','W','Th','F','Sa','Su'))

Does not work as intended for labeling. What's with the 0.5-width bins? And also, how to prevent the lack of gap between Sunday->0 and Monday->1? Ideally, no gaps between columns.

My data looks like:

> dat
  [1] "2010-04-02" "2010-04-06" "2010-04-09" "2010-04-10" "2010-04-14" "2010-04-15" "2010-04-19"
  [8] "2010-04-21" "2010-04-22" "2010-04-23" "2010-04-26" "2010-04-28" "2010-04-29" "2010-04-30"
 ...

> str(dat)
 Date[1:146], format: "2010-04-02" "2010-04-06" "2010-04-09" "2010-04-10" "2010-04-14" "2010-04-15" ...

> str(weekdays(dat))
 chr [1:146] "Friday" "Tuesday" "Friday" "Saturday" "Wednesday" "Thursday" "Monday" ...
> hist(weekdays(dat))
Error in hist.default(weekdays(dat)) : 'x' must be numeric

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

毁虫ゝ 2024-12-04 18:54:17
dat <- as.Date( c("2010-04-02", "2010-04-06", "2010-04-09", "2010-04-10", "2010-04-14", 
       "2010-04-15", "2010-04-19",   "2010-04-21", "2010-04-22", "2010-04-23","2010-04-24", 
        "2010-04-25", "2010-04-26", "2010-04-28", "2010-04-29", "2010-04-30"))
 dwka <- format(dat , "%a")
 dwka
# [1] "Fri" "Tue" "Fri" "Sat" "Wed" "Thu" "Mon"
#  [8] "Wed" "Thu" "Fri" "Sat" "Sun" "Mon" "Wed"
# [15] "Thu" "Fri"
dwkn <- as.numeric( format(dat , "%w") ) # numeric version
hist( dwkn , breaks= -.5+0:7, labels= unique(dwka[order(dwkn)]))

在此处输入图像描述

dat <- as.Date( c("2010-04-02", "2010-04-06", "2010-04-09", "2010-04-10", "2010-04-14", 
       "2010-04-15", "2010-04-19",   "2010-04-21", "2010-04-22", "2010-04-23","2010-04-24", 
        "2010-04-25", "2010-04-26", "2010-04-28", "2010-04-29", "2010-04-30"))
 dwka <- format(dat , "%a")
 dwka
# [1] "Fri" "Tue" "Fri" "Sat" "Wed" "Thu" "Mon"
#  [8] "Wed" "Thu" "Fri" "Sat" "Sun" "Mon" "Wed"
# [15] "Thu" "Fri"
dwkn <- as.numeric( format(dat , "%w") ) # numeric version
hist( dwkn , breaks= -.5+0:7, labels= unique(dwka[order(dwkn)]))

enter image description here

冬天的雪花 2024-12-04 18:54:17

我怀疑您想要一个条形图而不是直方图。您可以使用table来计算天数。

barplot(table(weekdays(dat)))

请注意,默认情况下,日期将按字母顺序排序,因此为了更自然地排序,您必须在因子调用中重新排序级别:

barplot(table(factor(weekdays(dat),levels=c("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"))))

I suspect you want a barplot rather than a histogram. You can use table to count the days.

barplot(table(weekdays(dat)))

Note that by default the days will be sorted alphabetically, so to order it more naturally you will have to reorder the levels in a factor call:

barplot(table(factor(weekdays(dat),levels=c("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"))))
夏夜暖风 2024-12-04 18:54:17

将您的 weekdays(dat) 转换为因子(分类变量的数据类型),并为直方图取消分类(将转换为整数)。因子类上有一些操作,可以轻松创建自定义 x 轴。

## days of the week
days <- c('Sun','Mon','Tues','Wed','Thurs','Fri','Sat')

## sample with replacement to generate data for this example
samples <- sample(days,100,replace=TRUE)

## convert to factor
## specify levels to specify the order
samples <- factor(samples,levels=days)

hist(unclass(samples),xaxt="n")
axis(1,at=1:nlevels(samples),lab=levels(samples))
box()

Convert your weekdays(dat) to a factor (data type for categorical variables), and unclass it (which will convert to integer) for the histogram. There are operations on the factor class which makes it easy to create the custom x-axis.

## days of the week
days <- c('Sun','Mon','Tues','Wed','Thurs','Fri','Sat')

## sample with replacement to generate data for this example
samples <- sample(days,100,replace=TRUE)

## convert to factor
## specify levels to specify the order
samples <- factor(samples,levels=days)

hist(unclass(samples),xaxt="n")
axis(1,at=1:nlevels(samples),lab=levels(samples))
box()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文