1

My data looks like:

composerName             season Location Time Venue eventType id conductorName interval movement workTitle
1 Anthem,                 1918-19       55   55    55        55 55            55       55       55        55
2 Beethoven,  Ludwig  van 1912-13       37   37    37        37 37            37       37       37        37
3 Beethoven,  Ludwig  van 1915-16       38   38    38        38 38            38       38       38        38
4 Beethoven,  Ludwig  van 1919-20       38   38    38        38 38            38       38       38        38
5 Beethoven,  Ludwig  van 1920-21       36   36    36        36 36            36       36       36        36
6 Beethoven,  Ludwig  van 1921-22       44   44    44        44 44            44       44       44        44

My code looks like this:

ggplot(aes(x = composerName, y = id, fill = season, label = composerName), data = music)+
  geom_bar(stat = 'identity')+
  geom_text(angle = 90, position = position_dodge(width = 1), hjust = -.1)

Which gives this:

enter image description here

I'd like it to include only one label per composer rather than a label for each season. How do I remove the duplicate labels?

snapcrack
  • 1,761
  • 3
  • 20
  • 40
  • 1
    Create a new data.frame where you have one label per composer and pass that to `geom_text(data = ...)`. – Roman Luštrik Aug 11 '17 at 07:55
  • 1
    E.g. `geom_text(data = dplyr::distinct(music, composerName))` should work. Also you can use `geom_col` instead of `geom_bar(stat = "identity")` – Axeman Aug 11 '17 at 07:57
  • @Axeman that gives me `Error: Cannot add ggproto objects together. Did you forget to add this object to a ggplot object?`. – snapcrack Aug 11 '17 at 08:03
  • Yeah, think that over for a second.. What is the error telling you? Hint: I did not give you a complete working piece of code. :) – Axeman Aug 11 '17 at 08:05
  • I can't show you how to do it because that data is not enough to make your plot, nor is it easy to copy with all those spaces. – Axeman Aug 11 '17 at 08:22

1 Answers1

4

Figured it out: I needed to include stat = 'identity' in geom_text():

ggplot(aes(x = composerName, y = id, fill = season, label = composerName), data = music)+
  geom_bar(stat = 'identity')+
  geom_text(stat = 'identity', angle = 90, 
            position = 'identity', hjust = -.2, check_overlap = TRUE)

enter image description here

snapcrack
  • 1,761
  • 3
  • 20
  • 40