0

I have below GGPLOT :

library(zoo)
library(quantmod)
library(ggplot2)
Data = data.frame('class' = c(rep(c('a', 'b', 'c'), 8), c('a', 'b')), 'Date' = as.yearqtr(rep(c('2021 Q4', '2022 Q1', '2022 Q2', '2022 Q3', '2022 Q4', '2023 Q1', '2023 Q2', '2023 Q3', '2023 Q4' ), each = 3))[-27], 'Value' = 1:26)
ggplot(Data, aes(x = Date, y = Value, fill = class)) +
    geom_bar(stat = 'identity', position = 'dodge', width = 0.1)

With this I am getting below plot :

enter image description here

As you see the first date is flowing out of the plot window.

Any pointer why is it happening and how to correct this will be very helpful.

Also, how can I get x-axis tick for all dates instead of having alternate dates?

Bogaso
  • 2,838
  • 3
  • 24
  • 54

2 Answers2

0

If you make Date a factor with factor(Date) you'll solve both of your problems. You'll notice you get really skinny bars. Just change your width argument inside geom_bar() to 0.5.

ggplot(Data, aes(x = factor(Date), y = Value, fill = class)) + geom_bar(stat = 'identity', position = 'dodge', width = 0.5)

Link to output plot

0

The issue comes from using as.yearqtr as explained here

ggplot(Data, aes(x = Date, y = Value, fill = class)) +
  geom_bar(stat = 'identity', position = 'dodge', width = 0.1) +
  scale_x_yearqtr(breaks = seq(from = min(Data$Date), to = max(Data$Date), by = 0.25), format = "%YQ%q")

enter image description here

user63230
  • 4,095
  • 21
  • 43