5

I'd like to put a label above only the modal bar (the tallest peak) on my geom_col plot, giving the x-axis value (CAG). Here's an example, but I can only get it to label every peak.

x <- seq(-20, 20, by = .1)
y <- dnorm(x, mean = 5.0, sd = 1.0)
z <- data.frame(CAG = 1:401, height = y)
ggplot(z, aes(x=CAG, y=height)) +
  geom_col() +
  geom_text(aes(label = CAG))

I'd be very grateful for help with labelling only the top peak

Mike
  • 541
  • 3
  • 14
  • 1
    Does this answer your question? [Add text labels at the top of density plots to label the different groups](https://stackoverflow.com/questions/57381627/add-text-labels-at-the-top-of-density-plots-to-label-the-different-groups) – tjebo Apr 17 '20 at 08:52
  • Thanks, though the {split/lapply/combine} strategy seems to be for a {geom_density} plot, which only requires one variable, rather than the two variables required for {geom_col}. I've tried applying it to my data, with the initial line being {sp – Mike Apr 17 '20 at 09:24
  • I think I've solved this now with: a % group_by(SampleFileName) %>% filter(Height == max(Height)) – Mike Apr 17 '20 at 09:38

1 Answers1

5

Just subset your dataset in geom_text to keep only the maximal value of y:

ggplot(z, aes(x=CAG, y=height)) +
  geom_col() +
  geom_text(data = subset(z, y == max(y)), aes(label = CAG))

enter image description here

dc37
  • 15,105
  • 3
  • 13
  • 29
  • Great, thanks. However, my real data is grouped, using fill and colour, which I imagine is why it's not working on that. Could I please ask if there's a simple edit that can be made to the subsetting to make it work on grouped data? – Mike Apr 17 '20 at 08:50
  • @Mike see the question I pointed to above (which is a duplicate) – tjebo Apr 17 '20 at 08:53
  • Thanks, though the {split/lapply/combine} strategy seems to be for a {geom_density} plot, which only requires one variable, rather than the two variables required for {geom_col}. I've tried applying it to my data, with the initial line being {sp – Mike Apr 17 '20 at 09:07
  • Glad that my answer help you ;). Based on your comments, it seems you succeed to apply this answer to multiple groups ;). If you feel satisfy by this answer, feel free to accept it in order to indicate that this question is solved. More information here;:https://stackoverflow.com/help/someone-answers – dc37 Apr 17 '20 at 16:45