NiuBiBang
NiuBiBang

Reputation: 628

ggplot2: geom_text label on ordered geom_bar

I am having trouble plotting value labels over ordered bars on a ggplot bar chart (I believe it is related to the fact that I am reordering the bars based on their value). Assistance is greatly appreciated. A dput() sample of my data is below, as well as the code I am currently using & the associated error message. Apologies in advance for the long 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

Upvotes: 1

Views: 2799

Answers (1)

Didzis Elferts
Didzis Elferts

Reputation: 98589

As you are using another data frame for the geom_text(), you should provide your x and y variables for geom_text() and also add inherit.aes=FALSE to geom_text() to ensure that variable names defined in ggplot() call are ignored for geom_text(). Also I think you need to use stat_summary() to show sum of values for each 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

Upvotes: 3

Related Questions