gpt4 book ai didi

r - 淡出面板的一侧以指示正在进行的时间

转载 作者:行者123 更新时间:2023-12-04 00:54:04 25 4
gpt4 key购买 nike

《金融时报》有一篇关于游戏机的有趣文章,“游戏结束了吗?”,其中有一个直方图来显示游戏机的时间线。

对于当前这一代(Xbox One 和 PS4,Switch),盒子的右侧是模糊的,表示“正在进行”。所有其他盒子都有硬边。

出于版权原因,我不会显示整个图表,但这是其中的一个片段。我相信该图是在 R 中使用 ggplot2 生成的。

Graph using blurred sides

我找不到如何为盒子实现这种效果 - 只有点。是否有几何或技巧来实现这种模糊边缘?

下面是用于说明的简单图形的代码:

library(tidyverse)
tribble(~device, ~start, ~end, ~num, ~off,
"x", 2015, 2020, 120, 0,
"y", 2016, 2022, 150, 120,
"z", 2017, 2023, 200, 270) %>%
ggplot() +
geom_rect(aes(xmin = start, xmax = end, ymin = off, ymax = off+num, fill = device)) +
geom_vline(aes(xintercept = 2020.5), lwd = 2, lty = 2) +
geom_label(aes(x = 2020.5, y = 0, label = "Today")) +
geom_text(aes(x = 2022, y = 320, label = "The right edge of the\nblue and green boxes\nshould be fuzzy or faded out...")) +
geom_text(aes(x = 2022, y = 90, label = "...but not the red box")) +
guides(fill = "none") +
theme_minimal()

Simple graphic to illustrate question谢谢,

优化

最佳答案

我采用了划分空间然后线性降低alpha的思路。

编辑:

添加用于生成新数据集和附加绘图的函数。此外,ggplot2 还新增了 guides 函数。

data_rect_blur <- function(data, step, edge) {
stopifnot(inherits(data, "data.frame"))
stopifnot(is.numeric(step))
stopifnot(is.numeric(edge))

require("dplyr", quietly = TRUE)
require("tidyr", quietly = TRUE)
require("ggplot2", quietly = TRUE)

data$edge <- edge
data$mm <- pmin(data$edge, data$end)
data$rest <- (data$end - data$edge)
data$rest_t <- data$rest > 0

rm_last <- function(x) {
x[-length(x)]
}

df2 <- data %>%
group_by(device) %>%
mutate(
seqe = list(seq(from = edge, to = if_else(rest_t, end, edge), by = step)),
seqe_s = list(rm_last(seq(from = edge, to = if_else(rest_t, end, edge), by = step))),
seqe_e = list((seq(from = edge, to = if_else(rest_t, end, edge), by = step))[-1]),
alps = list(seq(0.8, 0, length = length(seqe[[1]]) - 1))
)

df3 <- tidyr::unnest(df2, cols = c(seqe_s, seqe_e, alps))

res <- list(base = df2, add = df3)

res
}

df <- dplyr::tribble(
~device, ~start, ~end, ~num, ~off,
"x", 2015, 2020, 120, 0,
"y", 2016, 2022, 150, 120,
"z", 2017, 2023, 200, 270
)



gg_rect_blur <- function(df, step, edge) {
stopifnot(inherits(df, "data.frame"))
df_blur_list <- data_rect_blur(df, step, edge)
ggplot(df) +
geom_rect(data = df_blur_list$base, aes(xmin = start, xmax = mm, ymin = off, ymax = off + num, fill = device)) +
geom_rect(data = df_blur_list$add, aes(xmin = seqe_s, xmax = seqe_e, ymin = off, ymax = off + num, fill = device, alpha = alps))
}

gg_rect_blur(df, 0.1, 2020.05) +
geom_vline(aes(xintercept = 2020.5), lwd = 2, lty = 2) +
geom_label(aes(x = 2020.5, y = 0, label = "Today")) +
guides(fill = "none") +
geom_text(aes(x = 2022, y = 320, label = "The right edge of the\nblue and green boxes\nshould be fuzzy or faded out...")) +
geom_text(aes(x = 2022, y = 90, label = "...but not the red box")) +
theme_minimal() +
theme(legend.position = "none")
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union

reprex package 创建于 2021-07-07 (v2.0.0)

关于r - 淡出面板的一侧以指示正在进行的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64082084/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com