簡體   English   中英

如何使用ggplot2呈現計數條形圖,其中數據存儲在多列中?

[英]How to use ggplot2 to present bar graphs of counts where data stored in multiple columns?

我有一個描述傷害發生率的數據集,正在嘗試進行總結以進行初步分析。

傷害=

    ID    Injury.face    Injury.neck    Injury.chest    Injury.pelvis    Inj.loc.count
    1     Checked        Unchecked      Unchecked       Unchecked        1
    2     Unchecked      Checked        Unchecked       Checked          2
    3     Checked        Unchecked      Checked         Unchecked        2
    4     Unchecked      Checked        Checked         Checked          3
    5     Unchecked      Unchecked      Unchecked       Checked          1

我想使用ggplot2顯示數據,以便可以在條形圖中按位置查看傷害發生的頻率,並在Inj.loc.count列中對條形圖進行分面。

該數據集是一個較大表的子集,並且有10個傷害X列。 Inj.loc.count是每行中“已檢查”值的數量的計數。

我正在努力如何將每個傷害的計數顯示為X欄。

在提交此帖子之前,我的搜索都包含所有產生的帖子,其中要顯示在多個欄中的變量位於一列中。

抱歉,如果我在格式化時出錯,這是我的第一篇SO帖子。

提迪爾的方法是:

library(tidyr)

df <- structure(list(ID = 1:5, Injury.face = structure(c(1L, 2L, 1L, 
    2L, 2L), .Label = c("Checked", "Unchecked"), class = "factor"), 
    Injury.neck = structure(c(2L, 1L, 2L, 1L, 2L), .Label = c("Checked", 
    "Unchecked"), class = "factor"), Injury.chest = structure(c(2L, 
    2L, 1L, 1L, 2L), .Label = c("Checked", "Unchecked"), class = "factor"), 
    Injury.pelvis = structure(c(2L, 1L, 2L, 1L, 1L), .Label = c("Checked", 
    "Unchecked"), class = "factor"), Inj.loc.count = c(1L, 2L, 
    2L, 3L, 1L)), .Names = c("ID", "Injury.face", "Injury.neck", 
    "Injury.chest", "Injury.pelvis", "Inj.loc.count"), class = "data.frame", 
    row.names = c(NA, -5L))

new <- gather(df, key=c(ID, Inj.loc.count), Injury, -ID, -Inj.loc.count)
colnames(new) <- c("ID", "Inj.loc.count", "Name", "Injury")

> head(new, 10)
   ID Inj.loc.count        Name    Injury
1   1             1 Injury.face   Checked
2   2             2 Injury.face Unchecked
3   3             2 Injury.face   Checked
4   4             3 Injury.face Unchecked
5   5             1 Injury.face Unchecked
6   1             1 Injury.neck Unchecked
7   2             2 Injury.neck   Checked
8   3             2 Injury.neck Unchecked
9   4             3 Injury.neck   Checked
10  5             1 Injury.neck Unchecked


# count checked/unchecked injuries
n <- new %>% group_by(Name, Injury) %>% count

# join n to main df by Name and Injury type
new_df <- new %>% left_join(n, by=c("Name", "Injury")) %>% mutate(Name = gsub("Injury.","", Name))

# plot the data, faceted by Inj.loc.count
ggplot(new_df, aes(x = Name, y = n, fill = Injury)) +
      geom_bar(position = 'dodge', stat='identity') +
      facet_wrap(~Inj.loc.count, ncol = 1)+
      geom_text(aes(label=n), position=position_dodge(width=0.9), 
                    vjust=1.5, color = "white", fontface = "bold") +
      labs(y = "Number of cases")

在此處輸入圖片說明

在繪制之前,使用reshape2庫將其轉換為正確的格式。

reformatted <- melt(Injuries, id.vars = c("id")

然后將其輸入ggplot。 您將擁有一個可變列,可以在上面

暫無
暫無

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

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