R 按日期划分的离散数据的绘图计数

R 按日期划分的离散数据的绘图计数,r,dataframe,plot,ggplot2,R,Dataframe,Plot,Ggplot2,我不熟悉ggplot2,并尝试绘制一个连续的直方图,显示按日期和评级的评论演变 我的数据集如下所示: date rating reviews 1 2017-11-24 1 some text here 2 2017-11-24 1 some text here 3 2017-12-02 5 some text here 4 2017-11-24 3 some text here 5 2017-11-24 3 some text h

我不熟悉
ggplot2
,并尝试绘制一个连续的直方图,显示按日期和评级的评论演变

我的数据集如下所示:

        date rating reviews
1 2017-11-24      1 some text here
2 2017-11-24      1 some text here
3 2017-12-02      5 some text here
4 2017-11-24      3 some text here
5 2017-11-24      3 some text here
6 2017-11-24      4 some text here
我想要得到的是这样的东西:

对于
评级==1

        date    count
1  2017-11-24      2
2  2017-11-25      7
.
.
.
对于
rating==2
3

我试过了

ggplot(aes(x = date, y = rating), data = df) + geom_line()
但它只在y轴上给我评级,不计算:


您可以使用
dplyr
获取所需的数据集,并将其传递到
ggplot()

数据:


sample_data仅使用一些虚拟数据:

  library(tidyverse)
  set.seed(999)
  df <- data.frame(date = sample(seq(as.Date('2017/01/01'), as.Date('2017/04/01'), by="day"), 2000, replace = T),
             rating = sample(1:5,2000,replace = T))
  df$rating <- as.factor(df$rating)

  df %>%
  group_by(date,rating) %>%
  summarise(n = length(rating)) %>%
  ggplot(aes(date,n, color = rating)) +
  geom_line() +
  geom_point()
库(tidyverse)
种子集(999)
df%
总结(n=长度(评级))%>%
ggplot(aes(日期,n,颜色=评级))+
geom_线()+
几何点()

不清楚你在问什么。你想要一个每天按咆哮分组的直方图吗?比如
cumsum(rating)
?@Masoud是的,这正是我要找的
library(dplyr);编辑数据%group\u by(日期、评级)%%>%摘要(n=n())
库(ggplot2);ggplot(editeddata,aes(x=date,y=n,fill=rating))+geom_bar(stat='identity',position='stack')
它没有给我想要的图:/我要找的是一个有5行的图,每行代表一个等级(1..5),但是你建议只给我等级为keyCan的条你能说标准术语吗。你说的线是什么意思?我不知道什么是连续直方图。与此同时,我更新了我的答案,看看这是否符合你的期望。是的,我明白你的建议。我想要的是台词,而不是bars@saul也许这条
geom_线(aes(fill=as.factor(rating))
而不是
geom_线()
对你有用。它给了我一个带条的图,但我想成为一个带geom_线()的连续图对不起,我不明白;)。但是您仍然需要不同的分组(例如不同的颜色)对于每个评级,或者仅仅是每个日期收到的评论总数?是的,我想画5行,每行代表评级计数按日期的演变,如图所示
http://dhmontgomery.com/figure/dogrates/plot4-1.png
我只想要评级计数,而不是平均值
sample_data <- structure(list(id = c(1L, 2L, 2L, 3L, 4L, 5L, 5L, 6L, 6L, 1L,           
     2L, 3L, 3L, 4L, 5L, 6L, 1L, 2L, 2L, 2L, 3L, 4L, 5L, 6L), date = structure(c(1L, 
     1L, 3L, 7L, 1L, 1L, 1L, 1L, 5L, 2L, 3L, 8L, 8L, 3L, 4L, 5L, 5L,                 
     6L, 6L, 6L, 9L, 6L, 6L, 6L), .Label = c("2017-11-24", "2017-11-25",             
     "2017-11-26", "2017-11-27", "2017-11-28", "2017-11-29", "2017-12-02",           
     "2017-12-04", "2017-12-08"), class = "factor"), rating = c(1L,                  
     1L, 1L, 5L, 3L, 3L, 3L, 4L, 4L, 1L, 1L, 5L, 5L, 3L, 3L, 4L, 1L,                 
     1L, 1L, 1L, 5L, 3L, 3L, 4L), reviews = structure(c(1L, 1L, 1L,                  
     1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,                 
     1L, 1L, 1L, 1L, 1L), .Label = "review", class = "factor")), .Names = c("id",    
     "date", "rating", "reviews"), row.names = c(NA, 24L), class = "data.frame")   
  library(tidyverse)
  set.seed(999)
  df <- data.frame(date = sample(seq(as.Date('2017/01/01'), as.Date('2017/04/01'), by="day"), 2000, replace = T),
             rating = sample(1:5,2000,replace = T))
  df$rating <- as.factor(df$rating)

  df %>%
  group_by(date,rating) %>%
  summarise(n = length(rating)) %>%
  ggplot(aes(date,n, color = rating)) +
  geom_line() +
  geom_point()