0

my sample code- part of this question was answered by @r2evans.

    DF <- data.frame(Month = 1:12, Val = runif(12,5,35)) %>% 
          mutate(Month = month.name[Month])

ggplot(DF, aes(x = Month, y = Val))+
  geom_bar(stat="identity")

the ggplot produce the following with x-axis starting at different month. enter image description here

Mark
  • 7,785
  • 2
  • 14
  • 34
Hydro
  • 1,057
  • 12
  • 25
  • 5
    `... %>% mutate(Month = month.name[Month])` – r2evans Aug 22 '23 at 15:20
  • @r2evans I was thinking the ggplot `ggplot(DF, aes(x = Month, y = Val))+ geom_bar(stat="identity")` using the posted sample data would produce plot with `x-axis` starting from `January`. Its throwing random month on the `x-axis` – Hydro Aug 22 '23 at 15:28
  • 4
    (1) You originally asked *"How to change number to monthly name in R"*, so I answered that. (2) Every question that asks about ordering things in `ggplot2` comes down to `factor`. Change it to `mutate(Month = factor(month.name[Month], levels=month.name))`. – r2evans Aug 22 '23 at 15:37
  • 3
    For ggplot axis ordering, see https://stackoverflow.com/q/3253641/3358272, https://stackoverflow.com/q/12774210/3358272, https://stackoverflow.com/q/18401931/3358272; ordering with groups https://stackoverflow.com/q/44350031/3358272 – r2evans Aug 22 '23 at 15:37
  • 3
    The month names are _not_ random. They are in alphabetical order. You need to specify the order, as explained by @r2evans. – Allan Cameron Aug 22 '23 at 15:38
  • 2
    You should not completely change a question to get more information. If you have a new, different question; start a new post. – MrFlick Aug 22 '23 at 15:38
  • Thank you all for very helpful suggestions. @r2evan, I am not sure how do I accept your answer? I see there are multiple suggestion for having an answer to this question, I feel its a close match but not exactly what I was looking. So thinking it would still help others looking for solutions to similar problem. – Hydro Aug 22 '23 at 15:54
  • 2
    This question was originally a duplicate of https://stackoverflow.com/questions/50607659/convert-months-number-to-month-name, and when you changed it to ggplot2 axis order, the four links in my previous question are all applicable: the combination of `factor` and strictly the need for `levels=` are what you needed, and the first of those four links mention that. (Perhaps the 3rd/4th are less specific to your use here but still good info, imo.) – r2evans Aug 22 '23 at 16:20
  • 2
    That said, thanks for looking to "accept" something, but there's no need, the preferred resolution is close-as-dupe. – r2evans Aug 22 '23 at 16:22

0 Answers0