I've been through related questions and those answers aren't solving my problem of labels not dodging to match the geom_col
bars:
Data
x <- structure(
list(capacity = c(0, 0, 0, 2.1, 3.1, 4, 4.6, 5.6, 6,
1.9, 2.3, 3.8),
year = c("FY21", "FY21", "FY21", "FY21", "FY21",
"FY20", "FY20", "FY20", "FY20", "FY19", "FY19", "FY19"),
unified_date = structure(c(18536, 18567, 18597, 18628, 18659,
18567, 18597, 18628, 18659, 18536,
18567, 18597), class = "Date")),
row.names = c(NA, -12L), class = c("tbl_df", "tbl", "data.frame"))
Code
ggplot2::ggplot(x, aes(x = unified_date, y = capacity, fill = year)) +
geom_col(position = "dodge") +
geom_text(aes(label = capacity),
position = position_dodge(width = 1),
vjust = -0.5, size = 4)
Chart
I've tried adding fill = year
to the geom_text
aes
, or group = year
, moving aes
values around, variants on position_dodge()
- nothing.
Try this:
#Code
ggplot2::ggplot(x, aes(x = factor(unified_date), y = capacity, fill = year)) +
geom_bar(stat='identity',position = "dodge") +
geom_text(aes(label = capacity),
position=position_dodge(width=0.9), size = 4,vjust=-0.5)+
xlab('Date')
Output:
And if you want to work with the month properly try this:
#Code 2
x %>% mutate(Month=format(unified_date,'%b')) %>%
mutate(Month=factor(Month,levels = unique(Month),ordered = T)) %>%
ggplot2::ggplot(aes(x = Month, y = capacity, fill = year)) +
geom_bar(stat='identity',position = "dodge") +
geom_text(aes(label = capacity),
position=position_dodge(width=0.9), size = 4,vjust=-0.5)+
xlab('Date')
Output: