1

A few years ago a question was asked why geom_bar has trouble with yearmon. The proposed solution doesn't work any longer with an updated version of ggplot.

Here is a minimal example with ggplot 3.3.0:

library(zoo)
library(data.table)
library(ggplot2)


# geom_col can handle yearmon when only Jannuary data are conained
dt_tmp <- data.table(time_period = as.yearmon(c(2019, 2019, 2020)),
                     count_cases = c(1:2, 10), 
                     group = c("a", "b", "a"))

ggplot(dt_tmp, aes(x = time_period, y = count_cases, fill = group)) +
  geom_col() +
  scale_x_yearmon(breaks = sort(unique(dt_tmp$time_period)))


# however with anything esle than January geom_col has trouble
dt_tmp <- data.table(time_period = as.yearmon(c(2019, 2019, 2019 + 1/12)),
                       count_cases = c(1:2, 10), 
                       group = c("a", "b", "a"))

ggplot(dt_tmp, aes(x = time_period, y = count_cases, fill = group)) +
  geom_col() +
  scale_x_yearmon(breaks = sort(unique(dt_tmp$time_period)))

# however geom_line and geom_point can
ggplot(dt_tmp, aes(x = time_period, y = count_cases, colour = group)) +
  geom_line() +
  geom_point()

First plot

And once we include any other month than January we end up with:

Second plot

So what changed or what am I missing? Is this a bug?

Oliver
  • 312
  • 2
  • 9
  • 2
    Try `geom_col(orientation = "x")`. See [this comment](https://stackoverflow.com/questions/62272360/ggplot2-geom-col-not-working-for-non-integer-x-values#comment110134380_62272360) – Henrik Jun 17 '20 at 15:06
  • That is the answer. Thanks. I agree with "My taste would run to a much simpler rule: if one axis is a factor [or a yearmon], that determines orientation. If not, default to "x"." – Oliver Jun 17 '20 at 17:57

1 Answers1

0

Henrik gave the answer in his comment: geom_col(orientation = "x")

Oliver
  • 312
  • 2
  • 9