ggplot2条形图格式

时间:2017-08-07 21:09:53

标签: r ggplot2 format bar-chart

我一直在尝试将以下代码生成图像1,看起来像图像2,我非常感谢任何帮助!

barchart = ggplot(
  data = Final_tank, 
  aes(x = Lookback, y = Return, fill = Type)) +
geom_bar(aes(fill = Type), position = "dodge", stat="identity") + 
guides(
  fill = guide_legend(
    reverse = FALSE, 
    keywidth = 2, 
    keyheight = 2,
    nrow = nrow(Final_tank),
    labels = Final_tank$Category, 
    title = (NULL))) +  
scale_fill_manual(values = c("#002142", "#e8a823", "#afafaf")) +  
scale_y_continuous(labels = percent)

图片1 enter image description here

图片2 enter image description here

1 个答案:

答案 0 :(得分:0)

要使x轴按照您希望的顺序显示,您必须修改数据:

data %>% mutate(Lookback = fct_relevel(Lookback, c("Since Inception", "5 Year", ...)

其余:

+    
geom_text(aes(label = Return,
                      y = Return+0.015, x=Lookback),
                  size = 3, position = position_dodge(width=0.9)) +
        scale_y_continuous(labels = percent)+
        theme_bw()