有人可以帮助我吗?谢谢你
您可以使用包含方便ggplot2
geom_bar()
软件包。
示例一些数据:
# simulate some data
N <- 100
df <- data.frame(Date = sample(seq(as.Date("2000/1/1"), by = "month", length.out = 48), N, T),
Code = sample(c(022003, 396001, 441002), size = N, replace = T),
Number_of_events = rpois(N, 5))
#aggregate
count <- aggregate(.~ Date+Code,data=df,FUN=sum)
library(ggplot2)
ggplot(count, aes(x=Date, y=Number_of_events)) +
geom_bar(aes(fill=as.factor(Code)), color="black", stat="identity", position="stack")
我们可以使用
barplot
barplot(xtabs(Number_of_events~Code + Date, df),
beside = TRUE, legend = TRUE, col = c("red", "blue"))
或使用
ggplot
library(dplyr)
library(ggplot2)
df %>%
group_by(Date, Code) %>%
summarise(Number_of_events = sum(Number_of_events)) %>%
ggplot(., aes(x= Date, y = Number_of_events)) +
geom_bar(aes(fill= factor(Code)), position = "dodge", stat = "identity")