r - 从不同的变量中选择数据标签
问题描述
我正在创建一个瀑布图。我想在条形上方显示数据标签。我当前的代码是在基于变量“值”的位置显示数据标签。标签的位置应基于“结束”列。标签的值应基于变量“值”。
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"))
解决方案
你很亲近!一个问题是您正在根据“标签”设置百分比标签的 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"))
推荐阅读
- java - 如何在特定字符后修剪文本视图?
- google-cloud-platform - Google Cloud Storage 403 禁止使用服务帐户 - 在本地工作,但在部署为云功能时不能
- linux - 从 Linux 目录中的文件名中删除更改模式
- java - 是否可以并行运行递归函数?
- javascript - 为 SVG 多边形点添加句柄
- kotlin - Kotlin - 如何根据字符的总长度拆分字符串列表
- sql - 如果缺少某些值,如何完成 CONCAT - SQL?
- mysql - 获取表列中序列值的范围
- angular - 'app-line' 不是 Angular 中的已知元素,已在 @NgModule 中声明并导出
- casting - 如何在 DB2 中执行十进制数的乘除法时使用 Precision 和 Scalling