簡體   English   中英

ggplot2:排序的geom_bar上的geom_text標簽

[英]ggplot2: geom_text label on ordered geom_bar

我在ggplot條形圖上的有序條上繪制值標簽時遇到了麻煩(我認為這與我根據其值對條進行重新排序有關)。 非常感謝您的協助。 下面是我的數據的dput()示例以及我當前正在使用的代碼和相關的錯誤消息。 對於長dput()提前dput()

test<-structure(list(Area = c("East", "West", "East", "West", "West", "East", "West", "West", "West", "East", "East", "East", "East", "West", "East", "West", "West", "East", "West", "West", "West", "East", "East", "East", "West", "East", "West", "West", "East", "West", "West", "West", "East", "East", "East", "East", "West", "East", "East", "East", "East", "West", "East", "West", "West", "East", "West", "West", "West", "East", "East", "East", "East", "West", "East", "West", "West", "East", "West", "East", "West", "West", "East", "West", "West", "West", "East", "East", "East", "East", "West", "East", "East", "West", "East", "West", "West", "East", "West", "East", "West", "East", "West", "West", "East", "West"), OPReason = c("A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B",     
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "C",         
"C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C",   
"C", "C", "C", "C", "C"), Amount = c(825.68, 455.04, 347, 532, 323.2, 30.39, 557.58, 
281.89, 260, 574.77, 295.57, 536, 308.35, 681.46, 269.33, 0, 771, 803.4, 478.79, 
784.59, 310.5, 458.79, 281.14, 6861.51, 8261.71, 1725.06, 268, 1175.69, 1569.6, 
1771.88, 1537.5, 945.95, 1763.46, 2629.44, 2401.25, 947.2, 811.25, 669.13, 249.74, 332.98, 266.6, 531.88, 929, 1239.7, 831.3, 356, 385.35, 624, 348.5, 445.87, 301.49, 294.61, 329.35, 261.72, 480, 721.98, 1314.95, 538.75, 560.83, 695.56, 4743.72, 1718.76, 3792.76, 145.15, 3227.95, 2903.48, 1590, 853.13, 725.74, 549.36, 1250.5, 591.75, 591.75, 709.63, 381.63, 1034.25, 393.47, 912.71, 975.05, 1774.5, 638.25, 545.64, 1120, 625, 410.69, 600)), .Names = c("Area", "OPReason", "Amount"), class = "data.frame", row.names = c(NA, -86L))

# Create summary statistic of sum of Amount by Reason.
sums <- ddply(test,.(OPReason),summarize,tot=sum(Amount))

# Plot out above test as ordered bar chart. Running the code to geom_bar works 
# fine, but geom_text is not finding object 'Amount'.
  plot <- ggplot(test, aes(x=reorder(OPReason,Amount,function(x) -sum(x)),
                         y=(Amount/1000), fill=Area))+ 
        geom_bar(stat='identity')+
        geom_text(data=sums,stat='identity',aes(label=tot))
  plot
# This msg appears: Error in tapply(X = X, INDEX = x, FUN = FUN, ...) : 
# object 'Amount' not found

當你正在使用其他的數據幀的geom_text()您應提供變量x和y的geom_text()並添加inherit.aes=FALSEgeom_text()以確保在定義的變量名ggplot()調用將被忽略對於geom_text() 我也認為您需要使用stat_summary()來顯示每個Area的值總和。

ggplot(test, aes(x=reorder(OPReason,Amount,function(x) -sum(x)),
                         y=(Amount/1000), fill=Area))+ 
  stat_summary(fun.y=sum,geom="bar",position="stack")+
  geom_text(data=sums,aes(label=tot,x=OPReason,y=tot/1000),inherit.aes=FALSE)

在此處輸入圖片說明

暫無
暫無

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

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