plotting by month

In my data I have months April -October, and some values I want to plot by month on the x axis. R makes plots but the months are ordered alphabetically. How can I get them to be ordered by time like they actually occur?

attach(dat)
par(mfrow=c(3,2))
plot(Month,OTP, main="Scatterplot of OTP vs Month")
plot(Month,Trips, main="Scatterplot of  Trips vs Months")
plot(Month,Missed, main="Scatterplot of  Missed Trips by Month")
plot(Month,Early, main="Scatterplot of Early Quits by Month")
plot(Month,Late, main ="Scatterplot of Late Gates by Month")

some sample data:

Month Trips Missed Early Late Runs Closed Revenue VOM VAM

1 March 103,397 1636 483 1287 11,070 2629 992142 311 485

2 April 105,672 2043 407 1057 10,664 2701 997744 321 494

3 May 103,762 2464 465 848 10,691 2794 952024 304 505

4 June 108,791 3642 481 883 10,664 3181 940950 298 207

5 July 109,794 3706 461 931 11,014 3521 875145 284 507

6 August 112,084 4148 491 1024 10,816 3401 898512 284 507

OTP

1 0.8441

2 0.7994

3 0.7625

4 0.7124

5 0.7079

6 0.6751

I suggest converting your months column into a factor. {forcats} makes this simple.

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(forcats)

months = c("january", "february", "march", "april", "may", "june", 
           "july", "august", "september", "october", "november", "december")

set.seed(123)
dat = tibble(month = c("march", "april", "may", "june", "july", "august"),
             trips = rnorm(6) |> abs(),
             missed = rnorm(6) |> abs())

ggplot2::qplot(dat$month, dat$trips, geom = "col")

dat = mutate(dat, month = factor(month, levels = months))

ggplot2::qplot(dat$month, dat$trips, geom = "col")

Created on 2021-12-29 by the reprex package (v2.0.1)

Turn month into a factor variable and use the levels to set the order

# Sample data on a copy/paste friendly format, replace this with your own data frame
dat <- data.frame(
  stringsAsFactors = FALSE,
             Month = c("March", "April", "May", "June", "July", "August"),
             Trips = c(103397, 105672, 103762, 108791, 109794, 112084),
            Missed = c(1636, 2043, 2464, 3642, 3706, 4148),
             Early = c(483, 407, 465, 481, 461, 491),
              Late = c(1287, 1057, 848, 883, 931, 1024),
              Runs = c(11070, 10664, 10691, 10664, 11014, 10816),
            Closed = c(2629, 2701, 2794, 3181, 3521, 3401),
           Revenue = c(992142, 997744, 952024, 940950, 875145, 898512),
               VOM = c(311, 321, 304, 298, 284, 284),
               VAM = c(485, 494, 505, 207, 507, 507)
)

Sys.setlocale("LC_ALL","en_US.UTF-8") # Not needed if you are using an English locale
#> [1] "LC_CTYPE=en_US.UTF-8;LC_NUMERIC=C;LC_TIME=en_US.UTF-8;LC_COLLATE=en_US.UTF-8;LC_MONETARY=en_US.UTF-8;LC_MESSAGES=es_PE.UTF-8;LC_PAPER=es_PE.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=es_PE.UTF-8;LC_IDENTIFICATION=C"
dat$Month <- factor(dat$Month, levels = format(ISOdate(2004,1:12,1),"%B"))

attach(dat)
plot(Month,Trips, main="Scatterplot of  Trips vs Months")

Created on 2021-12-28 by the reprex package (v2.0.1)

Note: Next time please provide a proper REPRoducible EXample (reprex) illustrating your issue.

Using date objects makes this easier.

library(ggplot2)
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union
dat <- data.frame(
  Date = ymd("2021-03-01","2021-04-01","2021-05-01","2021-06-01","2021-07-01","2021-08-01"),
  Month = c("March", "April", "May", "June", "July", "August"),
  Trips = c(103397, 105672, 103762, 108791, 109794, 112084),
  Missed = c(1636, 2043, 2464, 3642, 3706, 4148),
  Early = c(483, 407, 465, 481, 461, 491),
  Late = c(1287, 1057, 848, 883, 931, 1024),
  Runs = c(11070, 10664, 10691, 10664, 11014, 10816),
  Closed = c(2629, 2701, 2794, 3181, 3521, 3401),
  Revenue = c(992142, 997744, 952024, 940950, 875145, 898512),
  VOM = c(311, 321, 304, 298, 284, 284),
  VAM = c(485, 494, 505, 207, 507, 507)
)

ggplot(dat,aes(Date,Trips)) + geom_line() + theme_minimal()

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.