1

Possible Duplicates: But all were explained for faceted bar plot, and mine is normal bar-chart Annotation of summary statistic on ggplot above bars on barchart, Annotation above bars:

I want to annotate maximum value above the bar. (Could use mean value in same case)

For Example (could skip), There was a game played at a school. Students have to throw water balloon at defined target. Each student got 4 chances and in each chance he/she can throw 10 balloons. At last, maximum from the four scores will be considered.

Data Set:

> Balloon_throw
    Student Score
1   Raju    4
2   Guddu   5
3   Pinky   4
4   Daina   6
5   Pappu   3
6   Raju    6
7   Guddu   5
8   Pinky   5
9   Daina   7
10  Pappu   4
11  Raju    5
12  Guddu   6
13  Pinky   4
14  Daina   6
15  Pappu   5
16  Raju    5
17  Guddu   7
18  Pinky   8
19  Daina   7
20  Pappu   5

I want to create a bar graph which show the maximum score obtained by particular student with a bar and only annotate his/her maximum score on top of the bar.

qplot(x = Student, y = Score, data= Balloon_throw, geom="bar", stat="summary", fun.y = "max", position="dodge", xlab="Student name", ylab = "Water Balloon hits", main = "Result of water balloon throwing game",fill= factor(Student))+geom_text(aes(label=Score, x = Student, y = Score), vjust = -0.5)

Annotation is shown top 3 values, I want the maximum score

Community
  • 1
  • 1
Ashvin Meena
  • 309
  • 1
  • 13

1 Answers1

1

Get only the Max. value

> Balloon_throw <- ddply(Balloon_throw, "Student", subset, Score==max(Score))
  Student Score
1   Daina     7
2   Daina     7
3   Guddu     7
4   Pappu     5
5   Pappu     5
6   Pinky     8
7    Raju     6

Remove the Duplicates

> Balloon_throw <- Balloon_throw[!duplicated(Balloon_throw),]
> Balloon_throw
  Student Score
1   Daina     7
3   Guddu     7
4   Pappu     5
6   Pinky     8
7    Raju     6
qplot(x = Student, y = Score, data= Balloon_throw, geom="bar", stat="summary", fun.y = "max", position="dodge", xlab="Student name", ylab = "Water Balloon hits", main = "Result of water balloon throwing game",fill= factor(Student))+geom_text(aes(label=Score, x = Student, y = Score), vjust = -0.5)

enter image description here

Prasanna Nandakumar
  • 4,295
  • 34
  • 63
  • 1
    I am not able to apply the same trick (jugaad) in case of mean. This is just a sample data, I want to learn the logic behind it. Could you please correct me at `geom_text` or suggest some alternative? – Ashvin Meena Apr 17 '15 at 11:32
  • I had mentioned in the question. I want to annotate maximum, mean, minimum value etc on top of bar. `ddply` is working fine in case of maximum and minimum, but isn't working for mean values. – Ashvin Meena Apr 17 '15 at 11:47
  • 1
    > Balloon_throw <- ddply(Balloon_throw,~Student,summarise,Score=mean(Score)) Student Score 1 Daina 6.50 2 Guddu 5.75 3 Pappu 4.25 4 Pinky 5.25 5 Raju 5.00 – Prasanna Nandakumar Apr 17 '15 at 11:54
  • @AshvinMeena i have given for mean - if this answer your ur question , select the answer as correct and upvote – Prasanna Nandakumar Apr 17 '15 at 11:56
  • It's okay to approve edits on your answer. It won't harm your reputation. :-) – Ashvin Meena Apr 23 '15 at 10:20
  • @AshvinMeena :) Its ok to accept the solution as answer. If you feel , it solved ur purpose. And its not in my control/hands to approve ur suggested edits.Not sure who has it. – Prasanna Nandakumar Apr 23 '15 at 13:01
  • I had up-voted your Answer&Comment because of your approach. But I got my answer without manipulating input data. `stat_bin` alternative of `geom_text` and `text` in case of normal `barplot`. So, I have left the question open for answers. – Ashvin Meena Apr 23 '15 at 13:42