Search code examples
rdatedataframe

Translate months in r from Italian to English


I'm plotting some data and I need to create a number of box-plots. The different category are months, and since I need to have them displayed in order and not into alphabetical order, I transformed the date into a factor with

dd.tot$month <- factor(format(dd.tot$month, "%b"), 
                       levels=format(ISOdate(2000, 1:12, 1), "%b"))

Since my system language (and mother tongue for that matter) is Italian, the month column in the dd.tot dataframe is in Italian. But I need them to use the English abbreviation (the paper is all in English and this will be the only part in Italian). I've tried to create a new vector with the different abbreviations to apply to the column with

 mymonths <- c("Jan","Feb","Mar",
               "Apr","May","Jun",
               "Jul","Aug","Sep",
               "Oct","Nov","Dec")

dd.tot$month <- mymonths[ dd.tot$month ]

But when I plot it with ggplot, they are displayed in alphabetical order. How do I translate them and have them as a factor?


Solution

  • you can set the local language to English. Just type this:

     Sys.setlocale("LC_TIME", "English")
    

    Then you do not have to translate them and you can order them like this:

    mymonths <- format(seq.Date(from = as.Date("2018-01-01"), to = as.Date("2018-12-01"), by = "m"), "%b")
    
    dd.tot$month <- factor(format(dd.tot$month, %b), levels = mymonths)
    

    If you want to put it back to Italian just type

    Sys.setlocale("LC_TIME", "Italian")