2017-08-11 216 views
0

我的數據是這樣的:刪除重複的標籤

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 

我的代碼如下所示:

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) 

其中給出這樣的:

enter image description here

我想它每個作曲家只包括一個標籤,而不是每個季節的標籤。我如何刪除重複的標籤?

+1

創建,你必須每一個作曲家標籤的新data.frame並傳遞給'geom_text(數據=。 ..)'。 –

+0

例如'geom_text(data = dplyr :: distinct(music,composerName))'應該可以工作。你也可以使用'geom_col'而不是'geom_bar(stat =「identity」)' – Axeman

+0

@Axeman給我'錯誤:不能將ggproto對象加在一起。你忘了把這個對象添加到ggplot對象嗎?'。 – snapcrack

回答

0

想通了:我需要包括stat = 'identity'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