首页 > 解决方案 > 从不同的变量中选择数据标签

问题描述

我正在创建一个瀑布图。我想在条形上方显示数据标签。我当前的代码是在基于变量“值”的位置显示数据标签。标签的位置应基于“结束”列。标签的值应基于变量“值”。

require(tidyverse)

df <- tibble(
  label = c("Forecast YTD", "Gross Accelerated Cashflows", "Gross Delayed Cashflows", 
            "Gross Asset Outperformance", "Gross Asset Underperformance"),
  value = c(0.664, 0.554, -0.515, 0.332, -0.034)) 

df <- df %>% 
  mutate(
    end = cumsum(value),
    start = dplyr::lag(end, default = 0),
    col = c("neg", "pos", "total")[c(3, (value[-1]>0)+1)],
    id = seq(1, n())
  ) %>% 
  bind_rows(summarise(., label = "Total Actual Collections", start = 0, 
                      end = last(end), id = n()+1L, col = "total"))


  df %>% ggplot(aes(x=label, y=value, fill = col)) + 
  geom_rect(aes(x = label, xmin = id -0.45, xmax = id + 0.45, ymin = end, ymax = start)) + 
  ylab("% Gross Cash Forecasted YTD") + xlab("") + 
  geom_text(aes(label = scales::percent(value, accuracy = 1)), vjust = -0.5)  +
  scale_y_continuous(labels = scales::percent) +
  theme(legend.position = "none") +
  scale_fill_manual(values = c("#bfbfbf", "#90a9e3", "#002060"))

标签: rggplot2

解决方案


你很亲近!一个问题是您正在根据“标签”设置百分比标签的 x 位置(geom_text从主调用继承此ggplot),而条形图的 x 位置是从“id”列计算的。由于ggplot根据标签按字母顺序排列 x 轴值,这会使您的轴与“id”中的值不同步。解决此问题的一种方法是将“标签”转换为有序因子。ggplot然后将获取订单数据,您的条形图和标签将同步。

另一个问题是标签的 y 值应设置为每列的“开始”或“结束”的最大值,以较大者为准。以下代码解决了这两个问题(注释行):

df <- tibble(
  label = c("Forecast YTD", "Gross Accelerated Cashflows", "Gross Delayed Cashflows", 
            "Gross Asset Outperformance", "Gross Asset Underperformance"),
  value = c(0.664, 0.554, -0.515, 0.332, -0.034)) 

df <- df %>% 
  mutate(
    end = cumsum(value),
    start = dplyr::lag(end, default = 0),
    col = c("neg", "pos", "total")[c(3, (value[-1]>0)+1)],
    id = seq(1, n()),
    max = pmax(start, end, na.rm = T) # the greater of "start" or "end" for each row
  ) %>%
  bind_rows(summarise(., label = "Total Actual Collections", start = 0, 
                      end = last(end), id = n()+1L, col = "total")) %>% 
  mutate(label = factor(label, label, ordered = T)) # convert labels to ordered factor

情节代码的一些小改动:

df %>% ggplot(aes(x=label, y=value, fill = col)) + 
  geom_rect(aes(x = label, xmin = id -0.45, xmax = id + 0.45, ymin = end, ymax = start)) + 
  ylab("% Gross Cash Forecasted YTD") + xlab("") + 
  geom_text(aes(label = scales::percent(value, accuracy = 1), y = max), vjust = -0.5)  +
  scale_y_continuous(labels = scales::percent) +
  theme(legend.position = "none") +
  scale_fill_manual(values = c("#bfbfbf", "#90a9e3", "#002060"))

在此处输入图像描述


推荐阅读