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 :
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?
CodePudding user response:
The issue comes from using as.yearqtr
as explained
CodePudding user response:
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)