Ashvin Meena
Ashvin Meena

Reputation: 309

Annotate values above bar - R

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

Upvotes: 1

Views: 995

Answers (1)

Prasanna Nandakumar
Prasanna Nandakumar

Reputation: 4335

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

Upvotes: 1

Related Questions