繁体   English   中英

使用csv文件中的特定行在R编程中制作条形图

[英]Making barchart in R Programming using specific rows from csv file

我想在 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

这是一个我认为可以满足您要求的示例。

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

结果条形图

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM