我有一个图表与条形图和折线图在一起。我没有问题,显示标签的酒吧,但我想显示他们也在顶部的折线图。这条线是显示的数量服务的总和为每个日期变量。下面是代码:
sample <- data.frame(service_type = c("A","B","C","A","B","C","A","B","C","A","B","C","A","B","C","A","B","C","A","B","C"),
qty = c(38,185,87,29,12,133,2,14,31,2,9,59,60,43,137,135,31,159,15,32,1),
year_week = c("2022 - CW02","2022 - CW02","2022 - CW02","2022 - CW03","2022 - CW03","2022 - CW03","2022 - CW04","2022 - CW04","2022 - CW04","2022 - CW05","2022 - CW05","2022 - CW05","2022 - CW06","2022 - CW06","2022 - CW06","2022 - CW07","2022 - CW07","2022 - CW07","2022 - CW08","2022 - CW08","2022 - CW08"),
xlabel2 = c("Jan-22","Jan-22","Jan-22","Jan-22","Jan-22","Jan-22","Jan-22","Jan-22","Jan-22","Feb-22","Feb-22","Feb-22","Feb-22","Feb-22","Feb-22","Feb-22","Feb-22","Feb-22","Feb-22","Feb-22","Feb-22")
)
sample <- sample %>%
mutate(date = as.Date(paste0(year_week, 1), "%Y - CW%U%u"))
ggplot(sample ) +
aes(x = date, y = qty, fill = service_type) +
geom_col(position = "dodge") +
geom_line(aes(group = 1), stat = "summary", fun = sum, linetype = "dashed", size = 1, color = '#FF507B') +
geom_text(aes(label=qty),
position=position_dodge(7 * 0.9), vjust = 0)+
scale_fill_manual(values = c(A = "#FFC000", B = "#92D050", C = "#AE5AFF"))+
scale_x_date(
date_labels = "%Y - CW%U",
date_breaks = "1 week",
sec.axis = dup_axis(
breaks = as.Date(paste0("15-", unique(sample$xlabel2)), "%d-%b-%y"),
labels = \(x) format(x, "%b-%y")
)
) +
theme_minimal() +
theme(legend.position = "left",
axis.text.x.bottom = element_text(angle=90, hjust=1, vjust = 0.5),
axis.title = element_blank(), legend.title = element_blank(),
panel.grid.minor.x = element_blank())
字符串
这个图表是这样的:
x1c 0d1x的数据
感谢大家分享一些建议!
1条答案
按热度按时间z9smfwbn1#
你可以添加标签的方式基本上与行相同。使用
geom_text
和stat="summary"
,并添加计算值作为标签使用label=after_stat(y)
。字符串
的数据