按日期排序的直方图

Histogram that's ordered by dates

我有一个名为 stepsperday 的数据集。

head(stepsperday)

给予

2012-10-01 2012-10-02 2012-10-03 2012-10-04 2012-10-05 2012-10-06 
         0        126      11352      12116      13294      15420

基本上,它有 30 天的数据,我想从中制作一个直方图。

然而,使用 qplot(stepsperday, geom="histogram") 给出

`stat_bin()` using `bins = 30`. Pick better value with `binwidth`

引入 bins = 30 并不能解决问题,我不确定它是否可以与 ggplot 一起使用。 使用 names(stepsperday) <- c(1:30) 更改日期也没有太大作用。

如何制作按天排序的直方图。

我想你想制作柱形图,而不是直方图。直方图总结了单个连续变量的分布,而柱形图显示了连续变量和分类变量之间的关系,这里是步骤和时间(天)。

从您发布的输出来看,stepsperday 似乎是一个 zoo 对象,而不是数据框中的列,并且 ggplot2 旨在处理数据框(或 tibbles)。如果是这样,那么您需要将其转换为数据框,然后生成您的绘图。类似于:

library(tidyverse)
library(zoo)
library(lubridate)

# make example zoo object to test
set.seed(123)
stepsperday <- zoo::zoo(rpois(30, lambda = 10000),
                        order.by = seq(from = lubridate::date("2020-01-01"),
                                       to = lubridate::date("2020-01-01") + 29,
                                       by = "day"))

stepsperday %>%
  # convert zoo object to data frame with date as a column
  data.frame(date = zoo::index(.), steps = .) %>%
  ggplot(aes(x = date, y = steps)) +
    geom_col()

生成的结果如下: