如何解决ggplot2中的y轴之和
exdataframe <- data.frame(
date = c("2020-10-18","2020-10-20","2020-10-21","2020-10-31","2020-10-29","2020-11-01","2020-11-02"),Salesman = c("Bob","Mark","Rob","Richard","Richard"),values = c(10,50,30,100,120,90,20)
)
exdataframe$date <- as.Date(exdataframe$date) # transform to Date class
所以我绘图:
ggplot(exdataframe,aes(x = cut(date,breaks = "month"),y = values,fill=Salesman)) +
geom_bar(stat = "identity",position=position_dodge()) +
geom_text(aes(label=values),position=position_dodge(width=0.9),vjust=-0.25) +
xlab("Month")
我的问题是如何按Salesman列和按月以及按Salesman分组求和所有Y轴值?
解决方法
我们可以按sum
library(dplyr)
library(lubridate)
library(ggplot2)
library(zoo)
exdataframe %>%
group_by(month = as.yearmon(date),Salesman) %>%
summarise(values = sum(values),.groups = 'drop') %>%
ggplot(aes(x = month,y = values,fill = Salesman,group = Salesman)) +
geom_bar(stat = 'identity',position = position_dodge())
-输出
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。