我正在尝试使用以下代码在堆积条形图上绘制频率
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
我可以在没有太多强制的情况下用ggplot
来绘制这个。是否可以在highcharter
中执行此操作而不需要太多强制(例如4个合并语句)。这个post的答案对我不起作用。
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())
答案 0 :(得分:4)
hchart
与qplot
类似。 hchart
尝试与qplot
具有相同的行为。
那么,这个怎么样?:
hchart(visits, "column", x = as.Date(VisitDate), y = freq, group = Clinic) %>%
hc_plotOptions(column = list(
dataLabels = list(enabled = FALSE),
stacking = "normal",
enableMouseTracking = TRUE)
)
希望得到这个帮助。