Dehua Liu
Dehua Liu

Reputation: 33

Display Date in ggplot as x axis

My data has the format of

["201301",1111],["201302",1111],["201702",2222],["201603",3333].

However, when I try to plot it as a bar chart, it looks not nice since the x value was considered as a number. There is a large gap between years. enter image description here

Is it possible to eliminate the gap?

Upvotes: 0

Views: 166

Answers (1)

Uwe
Uwe

Reputation: 42582

If I understand correctly, the OP wants to display monthly data where year and month are coded in the format "YYYYMM" in variable V1.

I can reproduce the issue by

# create sample data
years <- 2013:2017
DF <- data.frame(V1 = 100 * rep(years, each = 12) + rep(1:12, length(years)),
                 V2 = rep(1:12, length(years)))

library(ggplot2)
ggplot(DF, aes(V1, V2)) +
         geom_col()

enter image description here

To plot these monthly data, V1 needs to be converted into full-fledged dates, e.g., 201304 becomes the date 2013-04-01. So, each year-month is mapped to the first day of the month.

With help of the lubridate package we get

ggplot(DF, aes(lubridate::ymd(V1, truncated = 1L), V2)) +
  geom_col()

enter image description here

ggplot() recognizes that the x axis is now a Date class and scales accordingly. Using a Date scale has the benefit that it scales correctly even if data points are missing.

Upvotes: 1

Related Questions