2015-10-13 66 views
1

我有一個具有相同顏色框的箱型圖。我想用labs()命名該軸,並在該圖框的顏色中添加一個小符號。將顏色符號添加到軸標籤

sector <- c("HY", "HY (ETFs)", "IG", "IG (ETFs)", "Loan", "Equities") 
YTD_percent <- c(-0.2, 0.5, 0.05, 0.2, -0.1, 0.1) 
data1 <- data.frame(sector, YTD_percent) 

ggplot(data1, aes(sector, YTD_percent)) + 
    geom_bar(stat="identity", color="#00669C", fill="#00669C", width=0.7) + 
    labs(x = NULL, y = NULL, title = "Plot Title") + 
    coord_flip() + 
    theme_bw() + 
    theme(axis.text.y=element_blank(), 
     axis.ticks.y = element_blank() 
     )+ 
    guides(fill = guide_legend(keywidth = 1, keyheight = 1)) + 
    scale_y_continuous(breaks=pretty_breaks(), expand = c(.05, .05), labels=percent) + 
    geom_text(aes(label = sector , y = YTD_percent), hjust = ifelse(data_ff$YTD_percent >= 0, -0.1, 1.1), size=6) 

enter image description here

這是軸應該是什麼樣子到底:

enter image description here

回答

3

您可以在您的美學定位和調整傳說補一點點:

library(ggplot2) 
library(scales) 
ggplot(data1, aes(sector, YTD_percent, fill = "YTD (in %)")) + 
    geom_bar(stat="identity", color="#00669C", width=0.7) + 
    scale_fill_manual(values = c("YTD (in %)" = "#00669C")) + 
    labs(x = NULL, y = NULL, title = "Plot Title", fill = "") + 
    coord_flip() + 
    theme_bw() + 
    theme(axis.text.y=element_blank(), 
     axis.ticks.y = element_blank(), 
     legend.position = "bottom" 
)+ 
    guides(fill = guide_legend(keywidth = 1, keyheight = 1)) + 
    scale_y_continuous(breaks=pretty_breaks(), expand = c(.05, .05), labels=percent) + 
    geom_text(aes(label = sector , y = YTD_percent), hjust = ifelse(data1$YTD_percent >= 0, -0.1, 1.1), size=6) 

enter image description here