【问题标题】:How to plot a bar chart with each x-lab bar contain both positive and negative y values in ggplot r?如何绘制一个条形图,每个 x-lab 条在 ggplot r 中都包含正负 y 值?
【发布时间】:2017-04-28 19:41:07
【问题描述】:

我有一个这样的示例数据集:

df_test <- data.frame(
  proj_manager = c('Emma','Emma','Emma','Emma','Emma','Alice','Alice'),
  proj_ID = c(1, 1, 2, 2, 2, 3, 4), 
  stage = c('B','B','B','A','C','A','C'),
  value = c(15,15,20,20,20,70,5)
)

我按经理计算项目的总价值;以及按阶段/经理的项目数量,并使用“输入”表示:

input <- select(df_test, proj_manager, proj_ID, stage, value) %>%
  filter(proj_manager=='Emma') %>%
  do({
    proj_value_by_manager = sum(distinct(., proj_ID, value)$value);
    mutate(., proj_value_by_manager =  proj_value_by_manager)
  }) %>%
  group_by(stage) %>%
 mutate(count_proj = length(unique(proj_ID))) 

print(input)


proj_manager proj_ID  stage value proj_value_by_manager count_proj
        <fctr>   <dbl> <fctr> <dbl>                 <dbl>      <int>
1         Emma       1      B    15                    35          2
2         Emma       1      B    15                    35          2
3         Emma       2      B    20                    35          2
4         Emma       2      A    20                    35          1
5         Emma       2      C    20                    35          1

我希望绘制条形图,显示所选经理的项目摘要:

  1. X 轴使用舞台立柱
  2. 正 y 轴上的项目编号计数
  3. 负 y 轴上的项目值总和(但显示正值)
  4. 特定阶段的钢筋末端已连接

图片未使用示例数据,我期待 条形图 不是箱线图):

如何使用 ggplot 条形图实现这一点? 任何帮助将不胜感激!

【问题讨论】:

  • 我想我可以用 plotly 做
  • @TheBiro 情节很好,你能分享这些方法吗?谢谢!

标签: r ggplot2 bar-chart


【解决方案1】:

您将不得不为此努力,但这是一个开始(/30 只是为了使比例具有可比性,将其更改为您需要的)。

df <- data.frame(input)

commapos <- function(x, ...) {
  format(abs(x), big.mark = ",", trim = TRUE,
     scientific = FALSE, ...) }


ggplot (input, aes(stage), stat = "identity") + 
geom_bar()+geom_bar(aes(y=-proj_value_by_manager/30.0), 
stat = "identity", fill = "Blue") + scale_y_continuous(labels = commapos)

这给了你这个准系统图表。努力吧,我相信你可以轻松让它更漂亮......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-28
    • 2013-06-02
    • 1970-01-01
    相关资源
    最近更新 更多