1

I need to create a graph, which should be ordered chronologically according to the column Variac_2019.

Nonetheless, I want the x axis to be labelled as the column Mes1_Any. The problem is data RStudio is ordering the x axis alphabetically:

enter image description here

How can I get the x axis labelled as column Mes1_Any and ordered as column Variac_2019?

My dataset:

structure(list(Mes1_Any = c("Gen.2020", "Feb.", "Març", "Abril", 
"Maig", "Juny", "Jul.", "Ag.", "Set.", "Oct.", "Nov.", "Des.", 
"Gen.2021", "Feb.", "Març", "Abril", "Maig", "Juny", "Jul.", 
"Ag.", "Set.", "Oct.", "Nov.", "Des.", "Gen.2022", "Feb.", "Març", 
"Abril", "Maig", "Juny", "Jul.", "Ag.", "Gen.2020", "Feb.", "Març", 
"Abril", "Maig", "Juny", "Jul.", "Ag.", "Set.", "Oct.", "Nov.", 
"Des.", "Gen.2021", "Feb.", "Març", "Abril", "Maig", "Juny", 
"Jul.", "Ag.", "Set.", "Oct.", "Nov.", "Des.", "Gen.2022", "Feb.", 
"Març", "Abril", "Maig", "Juny", "Jul.", "Ag."), Variac_2019 = c("2020_01", 
"2020_02", "2020_03", "2020_04", "2020_05", "2020_06", "2020_07", 
"2020_08", "2020_09", "2020_10", "2020_11", "2020_12", "2021_01", 
"2021_02", "2021_03", "2021_04", "2021_05", "2021_06", "2021_07", 
"2021_08", "2021_09", "2021_10", "2021_11", "2021_12", "2022_01", 
"2022_02", "2022_03", "2022_04", "2022_05", "2022_06", "2022_07", 
"2022_08", "2020_01", "2020_02", "2020_03", "2020_04", "2020_05", 
"2020_06", "2020_07", "2020_08", "2020_09", "2020_10", "2020_11", 
"2020_12", "2021_01", "2021_02", "2021_03", "2021_04", "2021_05", 
"2021_06", "2021_07", "2021_08", "2021_09", "2021_10", "2021_11", 
"2021_12", "2022_01", "2022_02", "2022_03", "2022_04", "2022_05", 
"2022_06", "2022_07", "2022_08"), Territori = c("Catalunya", 
"Catalunya", "Catalunya", "Catalunya", "Catalunya", "Catalunya", 
"Catalunya", "Catalunya", "Catalunya", "Catalunya", "Catalunya", 
"Catalunya", "Catalunya", "Catalunya", "Catalunya", "Catalunya", 
"Catalunya", "Catalunya", "Catalunya", "Catalunya", "Catalunya", 
"Catalunya", "Catalunya", "Catalunya", "Catalunya", "Catalunya", 
"Catalunya", "Catalunya", "Catalunya", "Catalunya", "Catalunya", 
"Catalunya", "Espanya", "Espanya", "Espanya", "Espanya", "Espanya", 
"Espanya", "Espanya", "Espanya", "Espanya", "Espanya", "Espanya", 
"Espanya", "Espanya", "Espanya", "Espanya", "Espanya", "Espanya", 
"Espanya", "Espanya", "Espanya", "Espanya", "Espanya", "Espanya", 
"Espanya", "Espanya", "Espanya", "Espanya", "Espanya", "Espanya", 
"Espanya", "Espanya", "Espanya"), Alimentació = c(-4.76190476190477, 
-4.51745379876797, -0.196850393700776, -15.1950718685832, -20.4007285974499, 
-5.13089005235603, -3.37394564198689, -1.94174757281553, 3.94600207684319, 
-8.44564240790655, -7.68518518518518, 6.2227074235808, -11.175898931001, 
-2.87474332648872, 6.29921259842521, 1.95071868583161, -6.46630236794171, 
9.42408376963351, -0.749765698219307, 6.25674217907228, 15.3686396677051, 
-2.60557053009882, 3.42592592592592, 16.2663755458515, -6.99708454810496, 
1.5400410677618, 9.25196850393701, -1.74537987679672, -0.637522768670318, 
11.0994764397906, -2.43673851921276, 8.62998921251348, -6.66058394160584, 
-5.89410589410588, 0.595829195630571, -11.8181818181818, -18.3970856102004, 
-4.42211055276382, -4.65753424657533, -6.06653620352251, 1.24879923150818, 
-9.30434782608696, -3.59168241965974, 3.06122448979591, -16.514598540146, 
-5.79420579420579, 5.56107249255213, -0.707070707070712, -7.01275045537341, 
7.33668341708542, -2.46575342465754, 0.684931506849318, 9.12584053794427, 
-5.39130434782609, 7.65595463137998, 9.89795918367347, -9.85401459854014, 
-3.4965034965035, 6.05759682224429, -0.202020202020203, -0.637522768670318, 
10.5527638190955, -4.5662100456621, 5.7729941291585)), row.names = c(NA, 
-64L), class = c("tbl_df", "tbl", "data.frame"))

Current code:

plot1 <- ggplot(alimentacio, aes(x=Mes1_Any, y=Alimentació, color= Territori, group=Territori))+
  geom_line()
Limey
  • 10,234
  • 2
  • 12
  • 32
Maria
  • 173
  • 7
  • Post your existing code, and reproducible example data. – zx8754 Oct 24 '22 at 07:39
  • One option would be to convert your column to factor with the order of the levels set in your desired order. See e.g. [How do you specifically order ggplot2 x axis instead of alphabetical order?](https://stackoverflow.com/questions/12774210/how-do-you-specifically-order-ggplot2-x-axis-instead-of-alphabetical-order). – stefan Oct 24 '22 at 07:41
  • But the problem is that I have quite many levels. – Maria Oct 24 '22 at 07:49
  • No, the problem is that your dataset is poorly constructed relative to your objective. You need either to put the effort in to deal with the poorly constructed data or to put the effort in to convert your dataset to a more convenient form. Personally, I would covert your character representations of "month/year" to proper dates (you'll have to impute a day value, but that's not an issue) and then use that as your x-axis column. Also, note that your sample data is incrrectly formatted [Corrected by edit.]. – Limey Oct 24 '22 at 08:37
  • The reason your dataset is poorly constructed is that it contains no information about the ordering of values in `Mes1_Any` by time and this is *precisely* the information you need to construct the graph that you want. – Limey Oct 24 '22 at 08:39
  • Okey thank you. I already have a new column in my dataset with proper dates. But how can I use column `Mes1_Any` to label the x axis? – Maria Oct 24 '22 at 09:14
  • Well thank you. At the end I converted `Mes1_Any` into a factor variable setting its levels as unique: `alimentacio$Mes1_Any<- factor(alimentacio$Mes1_Any, levels=unique(alimentacio$Mes1_Any))` – Maria Oct 24 '22 at 09:22
  • `Me1_Any` is already being used to label the x axis in your image and code. It just doesn't contain the values OR labels (two different things) that you want. Either create an actual date variable (https://www.statmethods.net/input/dates.html) or turn the variable into a factor and order it appropriately as desired. – socialscientist Oct 24 '22 at 09:27
  • Hint: instead of those month abbreviations, use `m<- as.Date(paste(2021, 1:12, 1, sep = "-")); abb <- format(m, "%b")`. This gives 3 letters month names in your locale. Then, convert to factor with those levels. – Rui Barradas Oct 24 '22 at 09:36

1 Answers1

0

You can use convert Variac_2019 into a date using lubridate::ym(), then convert Mes1_Any into an ordered factor whose levels are based on the order of those dates.

require(lubridate)
require(ggplot2)
require(forcats)
require(dplyr)

## Convert Variac_2019 into data using `lubridate::ym()`
alimentacio <- mutate(alimentacio, Variac_2019 = lubridate::ym(Variac_2019))

## Convert Mes1Any into ordered factor with
## levels based on Variac_2019
alimentacio <- mutate(alimentacio,
                      Mes1_Any =
                        forcats::fct_reorder(Mes1_Any, Variac_2019) |> 
                        ordered()) # convert to ordered factor

ggplot(alimentacio,
       aes(x = Mes1_Any, y = Alimentació,
           color = Territori, group = Territori)
       ) +
  geom_line()

Created on 2022-10-24 with reprex v2.0.2

Captain Hat
  • 2,444
  • 1
  • 14
  • 31