簡體   English   中英

如何按收集日期(x 軸)和其他因素(R)plot % 陽性病例(y 軸)?

[英]How to plot % positive cases (y-axis) by collection date (x-axis) and by other factors (R)?

請幫忙! 我有需要盡快准備報告的案例數據,但無法正確顯示圖表。

從以 CollectionDate 作為案例“記錄”的數據集(即具有相同日期的多行意味着當天更多案例),我想顯示當天的陽性病例數/總(陽性 + 陰性)病例數作為百分比y 軸,收集日期沿 x 軸。 然后我想按地區細分。 目標是看起來像這樣,但根據每日陽性/測試次數,而不僅僅是陽性與陰性。 我還想在每個圖表上添加一條 20% 的水平線。

  • 我曾嘗試在 ggplot 之前、之中和之后對其進行操作:
    ggplot(df_final, aes(x =CollectionDate, fill = TestResult)) +
    geom_bar(aes(y=..prop..)) +
    scale_y_continuous(labels=percent_format())

這是,再次,關閉。 但是百分比是錯誤的,因為它們只是將當天的比例與所有天數而不是每天數相比較。

然后我嘗試在以下命令中使用tally()來嘗試按區域計數並聚合:

  df_final %>% 
  group_by(CollectionDate, Region, as.factor(TestResult)) %>% 
  filter(TestResult == "Positive") %>%
  tally()

我仍然無法正確繪制圖表。 建議?

快速瀏覽我的數據:

head(df_final)

我可以讓你走到一半(請參閱代碼中的注釋以進行澄清)。 此代碼用於每個區域每天的計數(為每個區域單獨繪制)。 我認為您也可以進一步調整以計算每個縣每天的計數; 整個 state 應該是小菜一碟。 祝你的報告好運。

rm(list = ls())

library(dplyr)
library(magrittr)
library(ggplot2)
library(scales)
library(tidyr) #Needed for the spread() function

#Dummy data
set.seed(1984)

sdate <- as.Date('2000-03-09')  
edate <- as.Date('2000-05-18')
dateslist <- as.Date(sample(as.numeric(sdate): as.numeric(edate), 10000, replace = TRUE), origin = '1970-01-01')

df_final <- data.frame(Region = rep_len(1:9, 10000), 
                 CollectionDate = dateslist, 
                 TestResult = sample(c("Positive", "Negative"), 10000, replace = TRUE))


#First tally the positve and negative cases
#by Region, CollectionDate, TestResult in that order
df_final %<>% 
  group_by(Region, CollectionDate, TestResult) %>%
  tally()


#Then
#First spread the counts (in n)
#That is, create separate columns for Negative and Positive cases
#for each Region-CollectionDate combination
#Then calculate their proportions (as shown)
#Now you have Negative and Positive 
#percentages by CollectionDate by Region
df_final %<>% 
  spread(key = TestResult, value = n) %>% 
  mutate(Negative = Negative/(Negative + Positive), 
         Positive = Positive/(Negative + Positive))



#Plotting this now
#Since the percentages are available already
#Use geom_col() instead of geom_bar()
df_final %>% ggplot() + 
  geom_col(aes(x = CollectionDate, y = Positive, fill = "Positive"), 
           position = "identity", alpha = 0.4) + 
  geom_col(aes(x = CollectionDate, y = Negative, fill = "Negative"), 
           position = "identity", alpha = 0.4) +
  facet_wrap(~ Region, nrow = 3, ncol = 3)

這產生: 繪圖

好吧,我不得不說我不是 100% 確定我得到了你想要的,但無論如何,這可能會有所幫助。

數據:由於您是新來的,我必須讓您知道,使用您的數據的簡單且可重復的版本將使我們的 rest 更容易回答。 為此,您可以模擬任何其他對象的數據框,或在其上使用 dput function。

library(ggplot2)
library(dplyr)

data <- data.frame(
    # date
    CollectionDate = sample(
        seq(as.Date("2020-01-01"), by = "day", length.out = 15),
        size = 120, replace = TRUE),
    # result
    TestResult = sample(c("Positive", "Negative"), size = 120, replace = TRUE),
    # region
    Region = sample(c("Region 1", "Region2"), size = 120, replace = TRUE)
)

有了這些數據,你就可以跟着做得到你想要的圖。

# General plot, positive cases proportion
data %>% 
    count(CollectionDate, TestResult, name = "cases") %>% 
    group_by(CollectionDate) %>% 
    summarise(positive_pro = sum(cases[TestResult == "Positive"])/sum(cases)) %>% 
    ggplot(aes(x = CollectionDate, y = positive_pro)) +
    geom_col() +
    geom_hline(yintercept = 0.2)  

在此處輸入圖像描述

#  positive proportion by day within region
 data %>% 
    count(CollectionDate, TestResult, Region, name = "cases") %>% 
    group_by(CollectionDate, Region) %>% 
    summarise(
        positive_pro = sum(cases[TestResult == "Positive"])/sum(cases)
    ) %>% 
    ggplot(aes(x = CollectionDate, y = positive_pro)) +
    geom_col() +
    # horizontal line at 20%
    geom_hline(yintercept = 0.2) +
    facet_wrap(~Region)

在此處輸入圖像描述

暫無
暫無

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

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