ggplot2:有序geom_bar上的geom_text标签

时间:2013-11-30 14:16:44

标签: r ggplot2 label

我无法在ggplot条形图上的有序条形图上绘制值标签(我认为这与我根据它们的值重新排序条形的事实有关)。非常感谢协助。我的数据的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

1 个答案:

答案 0 :(得分:2)

当您使用geom_text()的其他数据框时,您应该为geom_text()提供x和y变量,并将inherit.aes=FALSE添加到geom_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)

enter image description here