【问题标题】:ggplot2 Bar Chart Formattingggplot2条形图格式
【发布时间】:2017-08-07 21:09:53
【问题描述】:

我一直在尝试将以下代码生成图像 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

图 2

【问题讨论】:

  • 您能否使用 dput 提供一些数据?
  • ... + theme_classic()
  • See here 用于标记 ggplot2 条的众多问题之一

标签: r ggplot2 format bar-chart


【解决方案1】:

要让 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()

【讨论】:

    猜你喜欢
    • 2015-01-03
    • 2021-03-14
    • 2019-01-14
    • 2018-09-15
    • 2021-01-17
    • 1970-01-01
    • 2017-10-24
    • 2016-01-14
    • 1970-01-01
    相关资源
    最近更新 更多