2

How do I show all values on the x-axis? In my example, I can only see Jan2021, July 2021, and Jan 2022 on my x-axis. I want to show all months from the "Month B" column.

Side question: What is the best method of handling column names with spaces in them? 
Most of the time, using `Column Name` or as.name("Column Name") works, but sometimes I get an error when I use that.
library(tidyverse)
library(zoo) #for as.yearmon function

df3 <- structure(list(`Month B` = structure(c(2020.75, 2020.83333333333, 
2020.91666666667, 2021, 2021.08333333333, 2021.16666666667, 2021.25, 
2021.33333333333, 2021.41666666667, 2021.5, 2021.58333333333, 
2021.66666666667, 2021.75, 2021.83333333333, 2021.91666666667, 
2022, 2022.08333333333, 2022.16666666667), class = "yearmon"), 
    colA = c(777, 1111, 888, 999, 999, 1500, 1400, 1000, 1200, 
    999, 1200, 900, 1200, 1300, 777, 1200, 777, 900), colB = c(700, 
    800, 900, 700, 800, 900, 600, 500, 500, 600, 500, 500, 600, 
    111, 999, 999, 888, 777)), row.names = c(NA, -18L), class = c("tbl_df", 
"tbl", "data.frame"))

df3$`Month B` <- as.yearmon(df3$`Month B`)
ggplot(df3, aes(`Month B`)) +
  geom_line(aes(y = `colA`, colour = "colA")) +
  geom_line(aes(y = `colB`, colour = "colB")) +
  theme(axis.text.x = element_text(angle = 90)) + ggtitle("MyTitle")
OTA
  • 269
  • 2
  • 17

1 Answers1

1

Here is a suggestion:

Instead of yearmon() I used here dmy function from lubridate and applied it in ggplot with scale_x_date:

library(lubridate)
library(tidyverse)
df3 %>% 
  mutate(`Month B`=dmy(paste("01", as.character(`Month B`)))) %>% 
  ggplot(aes(`Month B`)) +
  geom_line(aes(y = `colA`, colour = "colA")) +
  geom_line(aes(y = `colB`, colour = "colB")) +
  scale_x_date(date_labels="%b %y",date_breaks  ="1 month")+
  theme(axis.text.x = element_text(angle = 90)) + ggtitle("MyTitle")

enter image description here

TarJae
  • 72,363
  • 6
  • 19
  • 66
  • 1
    The code ran perfectly yesterday, but today I get an error. The `Month B` mutated column is filled with NAs. I did not change anything. Do you know why I this might occur? Warning: Problem while computing `Month B = dmy(paste("01", as.character(`Month B`)))`. i All formats failed to parse. No formats found. Error in seq.int(r1$mon, 12 * (to0$year - r1$year) + to0$mon, by) : 'from' must be a finite number – OTA Apr 22 '22 at 15:14
  • 1
    Has `df3` changed? If not be sure to `library(lubridate)` is loaded, Or replace `mutate`in line 2 with `dplyr::mutate(..`. Please tell me if it not works. Because with the provided `df3`it works on my machine! – TarJae Apr 22 '22 at 15:20
  • Ah, you were right. I had to reset my computer earlier because R crashed, and it did not save one of the lines that converted my df3 into the correct format. It works now – OTA Apr 22 '22 at 15:25
  • 1
    In `df3` Month B is `yearmon` format. With ` mutate(`Month B` = paste("01", as.character(`Month B`))) %>%` we transform it to character and add `01` before; then we have character format of: `01 Okt 2020` e.g. `d`ay `m`onth `y`ear. With `dmy` function we then transform it to date format: ` mutate(`Month B` = dmy(`Month B`))` we get `2020-10-01`. `dmy(paste("01", as.character(Month B))` is the short form of these 2 mutate lines. – TarJae Apr 22 '22 at 15:27
  • 1
    Perfect. Good to hear. The final trick then is to use `scale_x_date(date_labels="%b %y",date_breaks ="1 month")+` which gives us format `Okt 20` in Month intervals! – TarJae Apr 22 '22 at 15:28
  • 1
    Got it. That line made more sense once I had df3 formatted correctly. I understand it now. Thank you – OTA Apr 22 '22 at 15:30