簡體   English   中英

r-使用Dplyr計算子組內的%

[英]r - Calculate % within a Sub Group using Dplyr

我想按年份繪制各種事件類型的相對死亡人數。

我可以使用ggplot中的各個方面進行處理,但正在努力根據事件,年份和無死亡人數來計算事件百分比。

Event Type Year  Fatalities  % by Event 
                             (calculated)
-----      ----  ----------  ---------- 
Storm      1980           5  12.5%
Storm      1981           9  22.5%
Storm      1982          15  37.5%
Storm      1983          11  27.5%
Ice        1980           7  70%
Ice        1981           3  30%

我有以下代碼來計算它,但是計算不能使用分母高得多的分母進行計算。

fatalitiesByYearType <- stormDF %>% 
    group_by(eventType) %>% 
    mutate(totalEventFatalities = sum(FATALITIES)) %>%
    group_by(year, add = TRUE) %>% 
    mutate(fatalitiesPct =  sum(FATALITIES) / totalEventFatalities)

我究竟做錯了什么?

我的圖表如下。 我將其包括在內,是因為我也很想看看是否有一種方法可以在ggplot中按比例顯示數據。

p <- ggplot(data = fatalitiesByYearType,
    aes(x=factor(year),y=fatalitiesPct)) 
p + geom_bar(stat="identity") +
    facet_wrap(.~eventType, nrow = 5) +
    labs(x = "Year", 
         y = "Fatalities",
         title = "Fatalities by Type")

也許我不明白您的問題,但是我們可以從這里開始:

library(dplyr)
library(ggplot2)

# here the dplyr part
  dats <- fatalitiesByYearType %>%
          group_by(eventType) %>% 
          mutate(totalEventFatalities = sum(FATALITIES)) %>%
          group_by(year, add = TRUE) %>% 
          # here we add the summarise
          summarise(fatalitiesPct =  sum(FATALITIES) / totalEventFatalities)     
     dats
# A tibble: 6 x 3
# Groups:   eventType [?]
  eventType  year fatalitiesPct
  <fct>     <int>         <dbl>
1 Ice        1980         0.7  
2 Ice        1981         0.3  
3 Storm      1980         0.125
4 Storm      1981         0.225
5 Storm      1982         0.375
6 Storm      1983         0.275

您可以清楚地將所有內容合並到唯一的dplyr鏈中:

# here the ggplot2 part     
    p <- ggplot(dats,aes(x=factor(year),y=fatalitiesPct)) + 
         geom_bar(stat="identity") +
         facet_wrap(.~eventType, nrow = 5) +
         labs(x = "Year", y = "Fatalities", title = "Fatalities by Type") +
         # here we add the % in the plot
         scale_y_continuous(labels = scales::percent)  

在此處輸入圖片說明


附帶數據:

fatalitiesByYearType <- read.table(text = "eventType year  FATALITIES  
                                   Storm      1980           5  
                                   Storm      1981           9  
                                   Storm      1982          15  
                                   Storm      1983          11  
                                   Ice        1980           7  
                                   Ice        1981           3  ",header = T)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM