【问题标题】:Group and Sum By Factor按因子分组和求和
【发布时间】:2014-08-17 14:23:29
【问题描述】:

我有这样的数据:

> head(df)
                  Date IsWin
20 2014-07-13 00:00:00  True
21 2014-08-01 00:00:00  True
22 2014-08-05 00:00:00 False
23 2014-06-28 00:00:00  True
24 2014-05-31 00:00:00  True
25 2014-06-06 00:00:00  True

我想按日期分组并按 IsWin 求和(应该是 1 或 -1 的因子)。

我已经阅读了这个,但它并没有真正处理因素,所以我不知道如何应用它How to group a data.frame by date?

最终,我想将分组和汇总的数据传递到条形图以显示输赢的数量,例如ggplot2 and a Stacked Bar Chart with Negative Values

以下输出的表格对查看我想要的内容非常有帮助;但是,我想将其转换为条形图以获得更好的视觉效果:

> table(df[,1],df[,2])

                      False True
  2014-05-25 00:00:00     1    0
  2014-05-29 00:00:00     1    0
  2014-05-30 00:00:00     2    0
  2014-05-31 00:00:00     0    1
  2014-06-06 00:00:00     0    1
  2014-06-13 00:00:00     1    0
  2014-06-14 00:00:00     0    1
  2014-06-18 00:00:00     1    0
  2014-06-19 00:00:00     0    1
  2014-06-23 00:00:00     1    0
  2014-06-24 00:00:00     1    0
  2014-06-25 00:00:00     1    0
  2014-06-27 00:00:00     0    1
  2014-06-28 00:00:00     1    2
  2014-07-02 00:00:00     1    0
  2014-07-11 00:00:00     1    0
  2014-07-13 00:00:00     0    2
  2014-07-31 00:00:00     0    1
  2014-08-01 00:00:00     0    1
  2014-08-05 00:00:00     1    0
  2014-08-07 00:00:00     1    0
  2014-08-12 00:00:00     0    1

这是我的实际结构:

df <- structure(list(Date = c("2014-07-13 00:00:00", "2014-08-01 00:00:00", 
"2014-08-05 00:00:00", "2014-06-28 00:00:00", "2014-05-31 00:00:00", 
"2014-06-06 00:00:00", "2014-06-14 00:00:00", "2014-05-25 00:00:00", 
"2014-06-24 00:00:00", "2014-06-28 00:00:00", "2014-05-30 00:00:00", 
"2014-06-18 00:00:00", "2014-07-02 00:00:00", "2014-07-11 00:00:00", 
"2014-05-29 00:00:00", "2014-06-19 00:00:00", "2014-07-31 00:00:00", 
"2014-06-27 00:00:00", "2014-06-23 00:00:00", "2014-05-30 00:00:00", 
"2014-07-13 00:00:00", "2014-08-12 00:00:00", "2014-06-13 00:00:00", 
"2014-06-25 00:00:00", "2014-06-28 00:00:00", "2014-08-07 00:00:00"
), IsWin = structure(c(2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L
), .Label = c("False", "True"), class = "factor")), .Names = c("Date", 
"IsWin"), row.names = 20:45, class = "data.frame")

【问题讨论】:

  • 我已经编辑了我的答案。

标签: r ggplot2 sum aggregate


【解决方案1】:

试试:

ddf2 = data.frame(with(df, table(Date, IsWin)))

ggplot(ddf2)+
    geom_bar(aes(x=Date, y=Freq, fill=IsWin), stat='identity', position='dodge')+
    theme(axis.text.x=element_text(angle=45, size=10, hjust=1, vjust=1))

编辑: 对于负柱:

ddf2$new = ifelse(ddf2$IsWin=='True', 1,-1)

ggplot(ddf2)+
    geom_bar(data=ddf2[ddf2$new>0,], aes(x=Date, y=Freq*new, fill=IsWin), stat='identity')+
    geom_bar(data=ddf2[ddf2$new<0,], aes(x=Date, y=Freq*new, fill=IsWin), stat='identity')+
    theme(axis.text.x=element_text(angle=45, size=10, hjust=1, vjust=1))

【讨论】:

  • 谢谢,但输出与我的预期不同。例如,查看 2014-06-28。我希望红色为负一,蓝色为2,因为那天的比分是2胜1负。基本上,我希望损失乘以负一,以便它们更明显地显示为损失(低于零值)。另外,我添加了df$Date &lt;- as.Date(df$Date),以便日期显示为日期。你能告诉我如何将损失乘以负1吗?
  • 注意:我试过df$IsWin &lt;- factor(df$IsWin, levels=c(-1,1)),但它会创建一个空图表。
  • 请在上面的答案中查看我的编辑。
【解决方案2】:

这个怎么样?您在 包中使用 group_by()。您可以按以下方式对数据进行分组。您可以总结(计算)每个日期存在多少 TRUE 和 FALSE。使用此数据框,您可以创建堆积条形图。

library(dplyr)
library(ggplot2)

### Create a sample data set
dates <- rep(c("2014-08-01", "2014-08-02"), each = 10, times = 1)
win <- rep(c("TRUE", "FALSE", "FALSE", "TRUE", "TRUE"), each = 1, times = 4)

foo <- data.frame(cbind(dates, win))
foo$dates <- as.character(foo$dates)

ana <- foo %>%
         group_by(dates, win) %>%
         summarize(count = n())

# ana
# Source: local data frame [4 x 3]
# Groups: date

#        dates   win count
# 1 2014-08-01 FALSE     4
# 2 2014-08-01  TRUE     6
# 3 2014-08-02 FALSE     4
# 4 2014-08-02  TRUE     6

bob <- ggplot(ana, aes(x=dates, y=count, fill=win)) +
         geom_bar(stat="identity") +
         scale_y_continuous(breaks = seq(0,10,by = 1))

更新选项

看到cmets后,我想出了这个主意。它有两个新东西。一种是在获胜条件为 FALSE 时将正值转换为负值。另一个是新的ggplot。我确信有更好的做事方式。但是,我想在这里贡献这个想法。

ana <- foo %>%
    group_by(dates, win) %>%
    summarize(count = n())

# If there is FALSE in ith row in the win column, make the value of ith row in the
# count column negative. If you can avoid a loop and achieve the same goal, that
# may be the best option. But, I do not have any ideas in my mind yet.

for(i in 1:nrow(ana)){

    if(ana$win[[i]] == "FALSE"){

    ana$count[[i]] <- -abs(ana$count[[i]])

    }
}

bob <- ggplot(data=ana, aes(x=dates, y=count, fill=win)) +
       geom_bar(stat="identity", position=position_dodge())

这是否满足您的要求?

【讨论】:

  • 谢谢;但似乎日期在 x 轴上重叠。
  • 我将日期设置为日期。但这搞砸了x轴。我在这里将日期更改为字符。在我的机器上,我不再有这个问题了。希望这对你有用。
  • 感谢您的更新。你知道如何将假胜利乘以负数吗?我希望将损失显示为负值,将胜利显示为正值,以使我的可视化更加全面。
  • 我刚刚更新了我的建议。这对你有用吗?
  • 一切顺利。如果只需要画图,这里不需要使用dplyr。另一种方法也可以。
猜你喜欢
  • 1970-01-01
  • 2018-01-25
  • 2020-09-16
  • 1970-01-01
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多