facet_wrap is unable to sort months name properly

I expect that facet wrap will sort the month name like Nov-14, Dec-14, Jan-15, Feb-15 rather than Dec-14, Feb-15, Jan-15, Nov-14 .

facet_wrap

TestData = tibble(WeekDay = c("Monday",  "Tuesday", "Monday",  "Tuesday", "Monday",  "Tuesday", "Monday",  "Tuesday", "Monday",  "Tuesday", "Monday",  "Tuesday", "Monday",  "Tuesday", "Monday",  "Tuesday"), 
NumberOfSales = c(99, 100, 115, 114, 105, 102, 110, 108, 105, 105, 102, 101, 104, 102,  85,  87), 
MonthName = c("Nov-14", "Nov-14", "Nov-14", "Nov-14", "Dec-14", "Dec-14", "Dec-14", "Dec-14", "Dec-14", "Dec-14", "Jan-15", "Jan-15", "Jan-15", "Jan-15", "Feb-15", "Feb-15"))

ggplot(data = TestData, mapping = aes(x = WeekDay, y = NumberOfSales)) + geom_point() + facet_wrap(~MonthName)

The levels will be ordered alphabetically unless you set the order differently. I ordered then with the levels argument of factor().

TestData = tibble::tibble(WeekDay = c("Monday",  "Tuesday", "Monday",  "Tuesday", "Monday",  "Tuesday", "Monday",  "Tuesday", "Monday",  "Tuesday", "Monday",  "Tuesday", "Monday",  "Tuesday", "Monday",  "Tuesday"), 
                  NumberOfSales = c(99, 100, 115, 114, 105, 102, 110, 108, 105, 105, 102, 101, 104, 102,  85,  87), 
                  MonthName = c("Nov-14", "Nov-14", "Nov-14", "Nov-14", "Dec-14", "Dec-14", "Dec-14", "Dec-14", "Dec-14", "Dec-14", "Jan-15", "Jan-15", "Jan-15", "Jan-15", "Feb-15", "Feb-15"))
library(ggplot2)
ggplot(data = TestData, mapping = aes(x = WeekDay, y = NumberOfSales)) + geom_point() + facet_wrap(~MonthName)

TestData$MonthName <- factor(TestData$MonthName, levels = c("Nov-14", "Dec-14", "Jan-15","Feb-15"))
ggplot(data = TestData, mapping = aes(x = WeekDay, y = NumberOfSales)) + geom_point() + facet_wrap(~MonthName)

Created on 2023-03-28 with reprex v2.0.2

2 Likes

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.