2015-04-23 35 views
3

我有以下ggplot2圖表。我不希望透明度值標籤。避免在ggplot2中應用alpha美學對geom_text

ggplot2 chart

代碼:

ggplot(test, aes(x = reorder(org, -as.numeric(level)), y = obsAvg, fill = level, alpha = round)) + 
    geom_bar(stat = "identity", position = "dodge") + 
    scale_fill_manual(values = c("#E69F00", "#56B4E9", "#009E73")) + 
    scale_alpha_manual(values = c(.5, .75, 1), guide = FALSE) + 
    labs(title = "Average Observation Score by Round", y = "", x = "", fill = "Group") + 
    theme_bw() + 
    geom_text(aes(label = round(obsAvg,1)), vjust = -.5, size = 4, fontface="bold", position = position_dodge(width = .9)) + 
    scale_y_continuous(limits = c(0,4), expand = c(0,0)) + 
    theme(legend.position="bottom") 

數據:

set.seed(1) 
test <- data.frame(
    org = rep(c("Mammals", "Cats", "Tigers", "Lions", "Cheetahs"), 3), 
    level = rep(c("Animals", "Family", rep("Species", 3)), 3), 
    group = rep("Cats",15), 
    round = rep(c("Round1", "Round2", "Round3"),5), 
    obsAvg = runif(15, 1, 4) 
) 

我試圖移動alpha = round是的geom_bar()的審美,但後來我失去了躲閃標籤:

without dodge

我怎樣才能複製上面的圖,但不適用的透明度審美到我的標籤?

回答

4

我會將aes(alpha=)移動到geom_bar,然後將aes(group=)添加到geom_text以恢復閃避。

ggplot(test, aes(x = reorder(org, -as.numeric(level)), y = obsAvg, fill = level)) + 
    geom_bar(aes(alpha=round), stat = "identity", position = "dodge") + 
    scale_fill_manual(values = c("#E69F00", "#56B4E9", "#009E73")) + 
    scale_alpha_manual(values = c(.5, .75, 1), guide = FALSE) + 
    labs(title = "Average Observation Score by Round", y = "", x = "", fill = "Group") + 
    theme_bw() + 
    geom_text(aes(label = round(obsAvg,1), group=round), vjust = -.5, size = 4, fontface="bold", position = position_dodge(width = .9)) + 
    scale_y_continuous(limits = c(0,4), expand = c(0,0)) + 
    theme(legend.position="bottom") 

enter image description here

這是一個相當情節。

+3

另一種方法是通過'geom_text'中的'alpha = NULL'顯式取消映射。 – joran

+0

此解決方案完美工作,謝謝。和joran的評論一樣,將'group = round'和'alpha = NULL'加入到'geom_text'中,並保持原樣。 –