I am trying to plot frequency on a stacked barchart with the following code
x = data.frame(
Clinic = c('A','A','A','A','A','A','B','B','B','B','B','C','C','C','C'),
Doctor = c('Kooner','Halliday','Katz','Alizadeh','Patel','Baxter','Kooner','Halliday','Patel','Katz','Alizadeh','Baxter','Katz','Patel','Alizadeh'),
VisitDate = c('2014-06-01','2014-06-01','2014-06-15','2014-07-01','2014-07-01','2014-07-01','2014-07-01','2014-07-01','2014-07-01','2014-08-01','2014-08-01','2014-07-01','2014-08-01','2014-09-01','2014-08-01')
)
allDates = data.frame(VisitDate=c('2014-06-01','2014-06-15','2014-07-01','2014-07-15','2014-08-01','2014-08-15','2014-09-01'))
library(plyr)
visits = plyr::count(x[,c(1,3)])
visits1 = merge(allDates,visits, all.x = TRUE)
library(highcharter)
hc = highchart() %>%
hc_chart(type = "column") %>%
hc_yAxis(title = list(text = "Visits")) %>%
hc_xAxis(categories = allDates$VisitDate) %>%
hc_plotOptions(column = list(
dataLabels = list(enabled = FALSE),
stacking = "normal",
enableMouseTracking = TRUE)
) %>%
hc_series(list(name="Clinic-A",data=merge(allDates,visits1[visits1$Clinic == "A", ], all.x = TRUE)[,3]),
list(name="Clinic-B",data=merge(allDates,visits1[visits1$Clinic == "B", ], all.x = TRUE)[,3]),
list(name="Clinic-C",data=merge(allDates,visits1[visits1$Clinic == "C", ], all.x = TRUE)[,3])
)
hc
I can plot this with ggplot
without much coercion. Is it possible to do this in the highcharter
without too much coercion(for example the 4 merge statements). The answer to this post doesn't work for me.
library(ggplot2)
library(scales)
ggplot()+
geom_bar(aes(y = freq, x = as.Date(VisitDate), fill = Clinic),data = visits, stat = "identity")+
theme(legend.position = "bottom", legend.direction = "horizontal", legend.title = element_blank())+
scale_x_date(date_breaks = "1 month")+
scale_y_continuous(breaks = pretty_breaks())
hchart
, used in dataframe is similar toqplot
.hchart
try to have the same behavior asqplot
.So, how about this?:
Hope this help.