簡體   English   中英

使用 ggplot2 的堆疊條形圖 - 數據可視化

[英]Stacked barplot using ggplot2 - data visualisation

我對 R 的經驗很少,正在嘗試使用 ggplot2 制作堆疊的條形圖。 我有 2 組 - 控制組和實驗組,以及 2 個選擇 - 紅色和綠色。 我不確定如何組織我的數據。

我的試驗中有 80 只動物(對照 n=40,實驗 n=40),它們可以選擇紅色和綠色底物,我記下它們選擇了哪種底物,這就是我試圖繪制的數據。

我基本上想要 x 軸上的“實驗”和“控制”,以及 y 軸上的選擇數量(例如,控制、紅色 n=20、控制、綠色 = 12 等)。

任何幫助,將不勝感激!

編輯添加:

這是它輸出的圖形

這是我正在使用的代碼(包括建議的調整):

 df <- data.frame(group = rep(c("control", "experimental"), each = 40), substrate = sample (c("red","green"), 80, TRUE)) ggplot(df, aes(x = group, y = substrate, fill = substrate)) + geom_bar(stat = "identity") + scale_fill_manual(values = c("red", "green"))

這是輸出:

structure(list(group = 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, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("control", "experimental"
), class = "factor"), substrate = structure(c(1L, 2L, 1L, 2L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 
2L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 
2L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 
1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 
1L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 2L), .Label = c("green", 
"red"), class = "factor")), class = "data.frame", row.names = c(NA, 
-80L))

df(behaviour) 的輸出 - 原始數據幀

structure(list(group = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 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 = c("Control", "Experimental"
), class = "factor"), substrate = 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, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Green", 
"Red"), class = "factor")), class = "data.frame", row.names = c(NA, 
-80L))


您可以擁有這樣的數據,每個觀察(即每只動物)占一行,並為每個記錄記錄組和基質:

df <- data.frame(group = rep(c("control", "experimental"), each = 40),
                 substrate = rep(c("green", "red", "green", "red"), c(10, 30, 27, 13)))

現在使用ggplot定義您的繪圖,將group指定為您的 x 軸,並將..count..作為您的 y 軸。 使用geom_bar獲取您要查找的堆疊條,最后使用scale_fill_manual設置顏色:

library(ggplot2)

ggplot(df, aes(x = group, y = ..count.., fill = substrate)) + 
  geom_bar(colour = "black") +
  scale_fill_manual(values = c("green", "red"))

在此處輸入圖片說明

您的數據:

behaviour=structure(list(group = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 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 = c("Control", "Experimental"
), class = "factor"), substrate = 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, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Green", 
"Red"), class = "factor")), class = "data.frame", row.names = c(NA, 
-80L))

我們可以將您的數據制成表格:

 table(behaviour$group,behaviour$substrate)                           
               Green Red
  Control         10  30
  Experimental    27  13

所以你只能用geom_bar 指定fill 或y。 在您的情況下,您指定填充, geom_bar()函數將為您進行計數:

ggplot(behaviour,aes(x=group,fill=substrate))+
geom_bar() + scale_fill_manual(values=c("#29c7ac","#c02739"))

在此處輸入圖片說明

暫無
暫無

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

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