[英]Add bar labels in ggplot2 R 3.0.3
我試圖產生一個計數標簽geom_bar()
圖表。 數據的格式為Factor w/ 2 levels "0","1"
現在,我有以下代碼,但未找到'count'
錯誤。
這是我最近嘗試的代碼:
ggplot(mb,
aes(x = Intervention_grp,
y = ..count..,)) +
geom_bar(aes(fill = Intervention_grp),
alpha = (1 - 0.618)) +
geom_text(aes(label=Intervention_grp),
vjust = -0.2)
Error in eval(expr, envir, enclos) : object 'count' not found
我在R Graphics Cookbook中看到以下代碼:
ggplot(cabbage_exp, aes(x=ineraction(Date, Cultivar), y=Weight)) +
geom_bar(stat="identity") +
geom_text(aes(label=Weight), vjust = -0.2)
所以我嘗試了類似的格式,只是沒有使用交互
ggplot(mb,
aes(x = Intervention_grp,
y = Intervention_grp)) +
geom_bar(stat = "identity") +
geom_text(aes(label=sum(as.numeric(Intervention_grp))),
vjust = -0.2)
我的結果是兩個條形圖,一個條形用於第0組,一個條形用於第1組,標簽為1519,在數據集中沒有多少觀察值。
如何正確放置帶有計數的標簽?
當我執行以下操作時:
ggplot(mb,
aes(x = Intervention_grp,
y = ..count..,)) +
geom_bar()
我得到一個在y軸上具有正確計數的合適的條形圖,我只想將它們放在條形圖本身上。
謝謝,
您可以先添加一個“ Count
列。 這就是plyr
軟件包:
mb <- merge(mb, ddply(mb, .(Intervention_grp), summarise, Count=length(Intervention_grp), by = 'Intervention_grp')
那你可以用
geom_text(aes(label = Count, y = Count), vjust = -0.25)
iris2 <- iris[sample(seq_len(150), 50), ]
iris2 <- merge(iris2, ddply(iris2, .(Species), summarise, Count = length(Species)), by = 'Species')
ggplot(iris2, aes(x = Species)) + geom_bar() + geom_text(aes(label = Count, y = Count), vjust = -0.25)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.