我想在R中创建一个条形图。每个条形图应显示一周中的每一天。周一,周二,周三,周四,周五,周六,周日。 我的CSV文件看起来像这样,一周中的每一天都有288行数据。我想在第三列的平均值上创建条形图(称为占用率)。有谁知道如何创建这样的条形图,我可以在其中指定某些行而不是列?
17/09/2018 00:00,00:02:26,37,47,146
17/09/2018 00:05,00:02:26,37,47,146
17/09/2018 00:10,00:02:26,37,47,146
17/09/2018 00:15,00:02:26,37,47,146
18/09/2018 00:00,00:03:34,26,20,214,0
18/09/2018 00:05,00:03:34,26,20,214,0
18/09/2018 00:10,00:00:28,5,0,28
18/09/2018 00:15,00:00:02,3,0,2
18/09/2018 00:20,00:00:01,3,0,1
答案 0 :(得分:0)
这是我认为可以满足您需求的示例。
## packages
library(lubridate) # for changing the dates to days of week
library(dplyr) # for reformatting the data
library(forcats) # for recoding factors
library(ggplot2) # for plotting
## import your data
dat <- data.frame(time = c("17/09/2018 00:00", "17/09/2018 00:05", "18/09/2018 00:00", "18/09/2018 00:05"),
occupancy = c(37, 37, 26, 26))
## get average occupancy and add new variable with day of the week
dat.n <- dat %>%
mutate(time = dmy_hm(time)) %>%
mutate(month = month(time)) %>%
mutate(day_month = mday(time)) %>%
mutate(day_week = wday(time)) %>%
group_by(month, day_month) %>%
summarize(avg_occupancy = mean(occupancy), day_week = unique(day_week)) %>%
mutate(day_week = fct_recode(as.factor(day_week), "Monday" = "2",
"Tuesday" = "3"))
## make barplot
ggplot(dat.n, aes(x = day_week, y = avg_occupancy)) +
geom_bar(stat = "identity") +
xlab("") +
ylab("Average Occupancy")