2017-03-03 60 views
0

我試圖找到一種方法來輕鬆地翻轉用geom_text()註釋的情節。問題是,如果我使用coord_flip(),標籤是沒有翻轉的。ggplot2 coord_flip()with geom_text

一個簡單的例子,如果我翻轉這樣的情節:加入coord_flip()

df <- count(diamonds, cut) 

ggplot(df, aes(x = cut, y = n, label = n)) + 
geom_bar(stat = "identity") + 
geom_text() 

ggplot not flipped

,我越來越: enter image description here

我知道,這可能在某些情況下,這是一種理想的行爲,但相反,我需要酒吧上的標籤與酒吧保持一致,看起來更像:

enter image description here

有沒有解決方法?

+0

爲了增強可讀性,爲什麼不離開文本中的水平?你可以做,例如,'ggplot(df,aes(x = cut,y = n,label = format(n,big.mark =「,」)))+ geom_bar(stat =「identity」)+ geom_text(aes(y = 0.5 * n),color =「white」)+ coord_flip()'。 – eipi10

回答

2

您可以在geom_text中使用angle審美,將其設置爲固定值。

ggplot(df, aes(x = cut, y = n, label = n)) + 
    geom_bar(stat = "identity") + 
    geom_text(angle = 270) + 
    coord_flip() 

enter image description here

相關問題