[英]Grouped bar plot in ggplot2
我正在嘗試使用長格式數據制作分組條形圖。
這是數據:
structure(list(group = c("group1", "group2", "group3", "group1",
"group2", "group1", "group1", "group1", "group4", "group1", "group4",
"group4", "group1", "group4", "group1", "group1", "group2", "group1",
"group4", "group2", "group4", "group2", "group3", "group3", "group1",
"group1", "group3", "group3", "group1", "group1", "group3", "group1",
"group4", "group3", "group3", "group1", "group2", "group1", "group4",
"group1", "group3", "group3", "group3", "group2", "group2", "group4",
"group3", "group3", "group3", "group2", "group3", "group2", "group1",
"group1", "group3", "group1", "group1", "group2", "group4", "group1",
"group4", "group1", "group1", "group4", "group1", "group3", "group4",
"group1", "group4", "group2", "group4", "group1", "group2", "group4",
"group1", "group4", "group1", "group2", "group1", "group1", "group1",
"group1", "group2", "group1", "group3", "group1", "group1", "group1",
"group3", "group4", "group1", "group3", "group1", "group3", "group4",
"group1", "group2", "group1", "group3", "group1"), category = c("category4",
"category5", "category2", "category4", "category3", "category6",
"category3", "category1", "category4", "category2", "category6",
"category6", "category5", "category5", "category4", "category4",
"category1", "category6", "category1", "category4", "category6",
"category6", "category2", "category6", "category3", "category2",
"category6", "category3", "category6", "category1", "category6",
"category2", "category2", "category2", "category5", "category1",
"category1", "category4", "category3", "category4", "category4",
"category5", "category1", "category3", "category5", "category2",
"category2", "category5", "category5", "category2", "category6",
"category6", "category5", "category1", "category4", "category3",
"category6", "category1", "category6", "category3", "category2",
"category2", "category3", "category2", "category2", "category5",
"category4", "category4", "category4", "category4", "category1",
"category5", "category6", "category5", "category4", "category5",
"category1", "category2", "category3", "category5", "category3",
"category2", "category4", "category6", "category4", "category6",
"category1", "category4", "category4", "category3", "category4",
"category5", "category5", "category6", "category4", "category3",
"category5", "category3", "category3", "category1"), count = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0)), row.names = c(NA,
-100L), class = c("tbl_df", "tbl", "data.frame"))
當我運行以下命令時:
pivot_sample %>%
ggplot(aes(x=group,fill=category))+
geom_bar()
stat_count()
默認 function 似乎與默認position="stack"
一起工作得很好但是,當我在下面的代碼中切換到position="dodge"
時:
pivot_sample %>%
ggplot(aes(x=group,y=count,fill=category))+
geom_bar(position = "dodge",stat = "identity")
我確信我缺少一些基本的東西,可以使用另一種觀點。 我需要對aes()
中的y=
參數使用count
function 嗎?
所有幫助將不勝感激!
OP,這里的簡單答案只是將position="dodge"
添加到您的原始 plot 代碼中,並且可以根據組美學(未指定,因此默認為 bar geom 使用fill
審美作為分組依據):
pivot_sample %>%
ggplot(aes(x=group, fill=category)) +
geom_bar(position='dodge')
原因是geom_bar
中stat
參數的默認選項是stat="count"
。 這將沿 y 軸計算所有觀察值和 plot 的“計數”。 要訪問它,您可以使用..
表示法: ..count..
,但geom_bar()
沒有必要。 因此,下面的代碼向您展示了一種長格式,它顯示了相同的 plot:
pivot_sample %>%
ggplot(aes(x=group, fill=category)) +
geom_bar(position='dodge', aes(y=..count..), stat="count")
請注意,您的數據框有一個名為“count”的列,但是當您指定和使用..count..
時, pivot_sample$count
不是訪問的內容。 在stat="count"
function 運行后訪問的結果。
當您使用stat="identity"
時發生了什么? 好吧, "identity"
統計數據在 y 軸上繪制了實際值。 您指定了y=count
,這意味着在每個分組和類別中繪制了pivot_sample$count
列的值。 stat="identity"
的geom_bar
與使用geom_col()
相同(在這種情況下應該使用),這將需要定義x
和y
美學。 在這種情況下,“身份”將導致 y 審美的所有值相加 - 或pivot_sample$count
。
在您使用stat="identity"
展示的 plot 中,您看到count
表示為條形高度,等於每個條形的所有pivot_sample$count
值的總和。 對於數據中的該列,您沒有很多值 = 1,這就是它看起來如此的原因。
請注意,使用stat="count"
的geom_bar()
計算觀察值,而stat="identity"
計算值的總和。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.