当我试图以这种方式创建散点图时,它会变成一条平坦的线.以下是我的数据的标题: see here

> head(seattle_thefts)
# A tibble: 6 × 2
  report_date_time    offense            
  <dttm>              <chr>              
1 2020-02-04 13:54:37 Motor Vehicle Theft
2 2020-02-04 13:11:35 Motor Vehicle Theft
3 2020-02-04 09:46:13 Motor Vehicle Theft
4 2020-02-03 16:02:33 Motor Vehicle Theft
5 2020-02-03 15:49:24 Motor Vehicle Theft
6 2020-02-03 13:16:00 Motor Vehicle Theft

如何按月或年 for each 事件创建一段时间的曲线图?例如,一个散点图,它显示在2020年1月,然后2020年2月发生了多少次,等等.

这只会创建一条平坦的直线:

ggplot(seattle_thefts, aes(x = report_date_time, y = offense)) +
  geom_point()

我try 将其更改为一个数值:

seattle_thefts$offense <- mutate(seattle_thefts, offense = 1)

seattle_thefts %>% 
  group_by(month = floor_date(report_date_time, 'month')) %>% 
  summarize(sum_of_thefts = sum(offense))

推荐答案

offensemutate更改为offense的方式看起来很奇怪,因为它将返回tibbledata.frame,具体取决于seattle_thefts的数据类型.试着在你做了"改变"之后看看seattle_thefts.

我刚刚在mutate()的常用语法中修改了你的mutate()

seattle_thefts <- seattle_thefts %>% mutate( offense = 1)

seattle_thefts %>% 
  group_by(month = floor_date(report_date_time, 'month')) %>% 
  summarize(sum_of_thefts = sum(offense)) %>%
  ggplot(aes(month,sum_of_thefts ))+
  geom_point()

这里有一个更短的替代方案,您不需要更改为数字,它只使用n()来计算每个分组的month的行数

seattle_thefts %>% 
  group_by(month = floor_date(report_date_time, 'month')) %>% 
  summarize(sum_of_thefts =n()) %>%
  ggplot(aes(month,sum_of_thefts ))+
  geom_point()

R相关问答推荐

如何使用TukeyHSD绘制事后概率热图

强制相关图以显示相关矩阵图中的尾随零

使用对管道内单元格的引用生成新变量

在R中列表的结尾添加数字载体

单击 map 后,将坐标复制到剪贴板

次级y轴R gggplot2

如何在R中添加截止点到ROC曲线图?

使用gcuminc,如何使用逗号格式化风险表?

如何将dygraph调用到R Markdown作为一个shiny 的react 对象的参数?

如何改变时间图R中的悬停信息?

在R中使用download. file().奇怪的URL?

R函数,用于生成伪随机二进制序列,其中同一数字在一行中不出现超过两次

如何使用ggplot对堆叠条形图进行嵌套排序?

如何读取CSV的特定列时,给定标题作为向量

安全地测试文件是否通过R打开

在纵向数据集中创建新行

如何使用前缀作为匹配来连接数据帧?

在R中使用列表(作为tibble列)进行向量化?

创建列并对大型数据集中的特定条件进行成对比较的更高效程序

为什么在写入CSV文件时Purrr::Pwalk不起作用