按工作日在r中排序2维表

时间:2016-04-14 18:29:44

标签: r ggplot2 geom-bar

我是R语言的新手,并试图使用ggplot绘制下表。我无法按照工作日的顺序绘制表格[Mon,Tue,Wed,...]

有没有办法对表格进行排序?

     UserType       Day   Freq
1    Customer    Friday 106810
2  Subscriber    Friday 260799
3    Customer    Monday  89034
4  Subscriber    Monday 253095
5    Customer  Saturday 203406
6  Subscriber  Saturday 175384
7    Customer    Sunday 190026
8  Subscriber    Sunday 160719
9    Customer  Thursday  73510
10 Subscriber  Thursday 269220
11   Customer   Tuesday  65035
12 Subscriber   Tuesday 270355
13   Customer Wednesday  63419
14 Subscriber Wednesday 273822

tableWeek<-table(data$usertype, data$weekday)
tablePlot<-as.data.frame(tableWeek) 
names(tablePlot)<-c("UserType","Day","Freq")
ggplot(tablePlot,aes(x=Day,y=Freq,fill=UserType))+geom_bar(position="dodge",stat="identity")

1 个答案:

答案 0 :(得分:0)

这帮助了我Reorder factor levels by day of the week in R

添加以下

dayLabs<-c("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday") 
tablePlot$Day <- factor(tablePlot$Day, levels= dayLabs)
tablePlot<-tablePlot[order(tablePlot$Day), ]