apexcharter
apexcharter copied to clipboard
Stacked Bar Chart by Week Ending Date - x-axis labels
I have a dataset showing cases by week ending date (epi curve), and I want the labels to be at each date with a tick mark right in the middle of the bar chart. Having trouble figuring it out, any help is appreciated.
Hello,
Here's an example :
library(cranlogs)
library(apexcharter)
library(dplyr)
library(lubridate)
# Download some data for the example
cdl <- cran_downloads(c("apexcharter", "billboarder"), from = "2022-07-01")
cdl %>%
# group by week and package
group_by(package, date = as.character(ceiling_date(date, unit = "week"))) %>%
summarise(count = sum(count)) %>%
# create a bar chart
apex(aes(date, count, fill = package), type = "column") %>%
# use stacked columns
ax_chart(stacked = TRUE) %>%
# customize x-axis
ax_xaxis(
tickPlacement = "on",
labels = list(rotateAlways = TRUE, style = list(colors = "#000")),
axisBorder = list(color = "#000"),
axisTicks = list(color = "#000")
)
Does it help you ?
Victor
Perfect, thanks. Didn't realize I needed to convert date to character. Love this package!
Yes the trick is to convert dates to characters to treat them as categorical variable, just be sure of the order of your data before plotting !