0

I am new to R language and was trying to plot following table using ggplot. I could not plot table using weekdays in order [ Mon,Tue,Wed,...]

Is there any way to sort table ?

     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")
rmsorPth
  • 111
  • 2
  • 11
  • 2
    Have you searched SO for this ? Here is a [link](http://stackoverflow.com/questions/10309564/reorder-factor-levels-by-day-of-the-week-in-r) that may help but there are a number of solutions to this. You may also want to look at packages like `lubridate` (for date / time handling), and `dplyr` or `data.table` (for working with data frames, including sorting). However, it is easy to do in base R. – steveb Apr 14 '16 at 18:38
  • 1
    Possible duplicate of [Order x axis day values in gggplot2](http://stackoverflow.com/questions/35976244/order-x-axis-day-values-in-gggplot2) – alistaire Apr 14 '16 at 19:08

1 Answers1

3

This helped me Reorder factor levels by day of the week in R

Added following

dayLabs<-c("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday") 
tablePlot$Day <- factor(tablePlot$Day, levels= dayLabs)
tablePlot<-tablePlot[order(tablePlot$Day), ]
Community
  • 1
  • 1
rmsorPth
  • 111
  • 2
  • 11