问题描述 投票:0回答:2
我想对事件数(y)的数量(y)与每个代码为序列的日期(x)(因此彩色条)的日期(x)进行barplot。知道事件在完全相同的日期中没有发生。

有人可以帮助我吗?谢谢你

您可以使用包含方便

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)

r bar-chart
2个回答
1
投票

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"))

enter image description here或使用ggplot


1
投票
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")

	

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.