Like a bar chart of the day of the week, and there are string labels

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

I have two different problems:

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

Alternatively, if I convert to numeric data, how do I 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'))

Not suitable for labeling. What about bins with a width of 0.5? And also, how to prevent the gap between Sunday-> 0 and Monday-> 1? Ideally, there are no spaces between columns.

My data is as follows:

> 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
+5
source share
3 answers
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

+7
source

I suspect you want barplota histogram instead. You can use tableto count days.

barplot(table(weekdays(dat)))

Please note that by default, days will be sorted alphabetically, so to order it more naturally, you will have to change the order of the levels when the factor is called:

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

Convert weekdays(dat)to a factor (data type for categorical variables) and release it (which will convert to an integer) for the histogram. There are operations in the factor class that make it easy to create a 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()
+3
source

All Articles