R语言 有没有一种方法,我可以做一个循环,将迭代通过每一天的一周?

3ks5zfa0  于 2023-05-04  发布在  其他
关注(0)|答案(1)|浏览(210)

我很想简化这部分代码,希望使用一个循环来迭代一周中的每一天,找到平均值的最大值,然后在注解y轴的位置向该值添加一个选定的数字。
我无法准确地描绘出它理想的样子,也许是这样的:

For Day in Weekday;
  (max(mean(columnname) + 10) AS new_variable

对不起,这可能很糟糕,但我只做了两个星期。任何帮助是赞赏!
以下是我的蛮力迫使它工作:

## average minutes worn per day

# find max of mean range

data_df %>% 
  group_by(weekday) %>% 
  summarize(max(mean(minutesworn)))

# plot

data_df %>% 
  group_by(weekday) %>% 
  summarize(mean_wear = mean(minutesworn)) %>% 
  ggplot(mapping = aes(x = factor(weekday, level =
                                c('Sunday', 'Monday', 'Tuesday',
                                  'Wednesday', 'Thursday', 'Friday',
                                  'Saturday')), y = mean_wear, fill = weekday)) +
geom_col() +
labs(title = "Minutes Worn by Weekday",
   caption = "Data Collected in 2016") +
xlab("Weekday") + ylab("Average Minutes Worn") +
annotate("text", x = "Friday", y = 1052, label = "Friday") +
annotate("text", x = "Saturday", y = 1022, label = "Saturday") +
annotate("text", x = "Sunday", y = 977, label = "Sunday") +
annotate("text", x = "Monday", y = 1040, label = "Monday") +
annotate("text", x = "Tuesday", y = 1057, label = "Tuesday") +
annotate("text", x = "Wednesday", y = 1010, label = "Wednesday") +
annotate("text", x = "Thursday", y = 1008, label = "Thursday")

数据:

data_df <- tibble::tribble(
  ~id,        ~activitydate,         ~totalsteps, ~totaldistance,   ~sedentaryminutes, ~calories, ~activeminutes, ~totalminutesasleep, ~totaltimeinbed, ~timeawakeinbed, ~month,  ~weekday,    ~minutesworn, ~alldaywear,
  1503960366, as.Date("2016-04-12"), 13162,       8.5,              728,               1985,      366,            327,                 346,             19,              "April", "Tuesday",   1094,         TRUE,
  1503960366, as.Date("2016-04-13"), 10735,       6.96999979019165, 776,               1797,      257,            384,                 407,             23,              "April", "Wednesday", 1033,         TRUE,
  1503960366, as.Date("2016-04-15"), 9762,        6.28000020980835, 726,               1745,      272,            412,                 442,             30,              "April", "Friday",    998,          TRUE,
  1503960366, as.Date("2016-04-16"), 12669,       8.15999984741211, 773,               1863,      267,            340,                 367,             27,              "April", "Saturday",  1040,         TRUE,
  1503960366, as.Date("2016-04-17"), 9705,        6.48000001907349, 539,               1728,      222,            700,                 712,             12,              "April", "Sunday",    761,          TRUE,
  1503960366, as.Date("2016-04-19"), 15506,       9.88000011444092, 775,               2035,      345,            304,                 320,             16,              "April", "Tuesday",   1120,         TRUE,
  1503960366, as.Date("2016-04-20"), 10544,       6.67999982833862, 818,               1786,      245,            360,                 377,             17,              "April", "Wednesday", 1063,         TRUE,
  1503960366, as.Date("2016-04-21"), 9819,        6.34000015258789, 838,               1775,      238,            325,                 364,             39,              "April", "Thursday",  1076,         TRUE,
)
hi3rlvi2

hi3rlvi21#

下面是我如何清理你的代码:

days = c('Sunday',
         'Monday',
         'Tuesday',
         'Wednesday',
         'Thursday',
         'Friday',
         'Saturday')

plot_data = data_df %>%
  group_by(weekday) %>%
  summarize(mean_wear = mean(minutesworn)) %>%
  mutate(weekday = factor(weekday, levels = days)) 
  
ggplot(
  plot_data,
  mapping = aes(
    x = weekday,
    y = mean_wear, 
    fill = weekday
  )) +
  geom_col() +
  geom_text(aes(label = weekday), nudge_y = 10, vjust = 0) +
  labs(
    title = "Minutes Worn by Weekday",
    caption = "Data Collected in 2016",
    x = "Weekday",
    y = "Average Minutes Worn"
  )

相关问题