2016-09-15 159 views
0

我有這個柱狀圖中設置GGPLOT2標籤底色

group = c("A","A","B","B") 
value = c(25,-75,-40,-76) 
day = c(1,2,1,2) 
dat = data.frame(group = group , value = value, day = day) 
dat 
ggplot(data=dat, aes(x=factor(group), y=value, fill=factor(day))) + 
    geom_bar(stat="identity", position="dodge")+ 
    geom_label(aes(label =round(value,0),fill="white"), 
    colour = "black", position= position_dodge(width=1)) 

我想的標貼是白色的研究背景與黑色字體,但是當我添加fill="white"情節不正確。標籤沒有黑色字體的白色背景。

通知這裏沒有fill="white"情節看起來不錯。我只是想改變標籤的背景和字體

group = c("A","A","B","B") 
value = c(25,-75,-40,-76) 
day = c(1,2,1,2) 
dat = data.frame(group = group , value = value, day = day) 

ggplot(data=dat, aes(x=factor(group), y=value, fill=factor(day))) + 
    geom_bar(stat="identity", position="dodge")+ 
    geom_label(aes(label =round(value,0)),colour = "black", 
    position= position_dodge(width=1)) 

還要注意

如果我提出fill="white"aes()之外,那麼標籤不是在酒吧,但彼此堆疊。即它抵消了position=position_dodge(width=1)的影響,我需要通過酒吧的標籤

謝謝。

回答

4

做兩個修改:

  1. 移動fill = factor(day)aes()geom_bar
  2. group = factor(day)內的geom_label

如下所示:

ggplot(data=dat, aes(x=factor(group), y=value)) + 
    geom_bar(aes(fill = factor(day)), stat="identity", position="dodge")+ 
    geom_label(aes(label =round(value,0), group = factor(day)),colour = "black", position= position_dodge(width=1)) 

enter image description here

+0

如果我將填充=「白色」移動到aes之外,則標籤不會粘在條上,而會堆疊在另一個上。即它否定position = position_dodge(width = 1)的效果,我需要標籤在條上 – user3022875

+0

@ user3022875請參閱修復(使用#2) –

+0

謝謝......還有一個問題---你會如何現在改變背景和字體.....讓我們說你想要綠色背景和藍色字體。我問,因爲當我把fill = green int geom_label(aes(... fill =「green」))我得到一個錯誤...謝謝 – user3022875