0

I'm trying to label the top of my geom_bar with the # of samples for my data set The following link brought me close to my solution... Use stat_summary to annotate plot with number of observations I've used in sucessfully with a geom_bar but it won't work with when I add fill. The error I get is "Error in eval(expr, envir, enclos) : object 'variable' not found". Not sure why this is or what it means?

Here is my code:

#df<-original data frame
#df2<-Averaged Count Data/# of samples within groups

###Create Labels
labels<-table(df$Distance,df$Site)
meds<-c(by(df2$count,df2$Site, max))
+geom_text(data=data.frame(), 
aes(x=names(meds), y=meds,label=paste("n =",labels)),vjust=-2)

###Plot

ggplot(df, aes(x=Distance, y=count, fill=variable))+geom_bar(stat="Identity")+
geom_text(data=data.frame(),
aes(x=names(meds), y=meds, label=paste("n =",labels)),vjust=-2)

enter image description here

Essentially this picture but with colors filled in. I want the colours to represent my factors in my variable category

df
     Station    Site    Distance
           1           1    0
           2           1    20
           3           1    40
           4           1    60
           1           2    0
           2           2    20
           3           2    40
           4           2    60
           5           2    80

 df1
  Station   Site    Distance    Count   variable
     1         1    0          1        a
     2         1    20         1        a
     3         1    40         1        a
     4         1    60         1        a
     1         2    0          1        a
     2         2    20         2        a
     3         2    40         2        a
     4         2    60         2        a
     5         2    80         2        a
     1         1    0          0        b
     2         1    20         0        b
     3         1    40         0        b
     4         1    60         0        b
     1         2    0          0        b
     2         2    20         1        b
     3         2    40         1        b
     4         2    60         1        b
     5         2    80         1        b

Thanks!

Community
  • 1
  • 1
jmm181
  • 33
  • 4
  • 1
    do you have a variable named `variable`? what is you fill variable may be `Site`? – Ananta Nov 01 '13 at 11:57
  • 1
    Your question could be improved by providing a reproducible example. You can read more about it here: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example. – Roman Luštrik Nov 01 '13 at 12:00
  • This looks good so far, can you print the output of head(df)? – colcarroll Nov 01 '13 at 13:49
  • Here is the example of my data. The goal is to have labels of just the # of samples without labels for each fill color – jmm181 Nov 03 '13 at 15:17

0 Answers0