dreamRs / apexcharter

:bar_chart: R Htmlwidget for ApexCharts.js
https://dreamrs.github.io/apexcharter
Other
138 stars 15 forks source link

Stacked Bar Chart by Week Ending Date - x-axis labels #60

Closed ericmshearer closed 1 year ago

ericmshearer commented 1 year ago

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.

pvictor commented 1 year ago

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")
  )

image

Does it help you ?

Victor

ericmshearer commented 1 year ago

Perfect, thanks. Didn't realize I needed to convert date to character. Love this package!

pvictor commented 1 year ago

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 !