gpt4 book ai didi

r - 通过将点包装在一个框中来注释 ggplot2

转载 作者:行者123 更新时间:2023-12-01 13:19:43 25 4
gpt4 key购买 nike

我有一个看起来像这样的图(下面生成它的代码):

Movement of bits using one-at-a-time-hashing

我想知道是否有一种简单的方法可以使它看起来像这样:

Highlighted input bytes

即,我想通过在它们周围放置一个框来突出显示四行中的八个位置。

如果您有兴趣,该图显示了如何使用一次一个哈希函数在四个字节上移动位,所以我想突出显示四个输入字节。我在其他各种图中叠加不同的位模式,显示不同的位如何依赖于函数参数中的其他位和输入位。所以我必须生成同一个图的多个版本,因此我更愿意不必为每个图都注释 PDF 文件。

library(dplyr)
library(purrr)
library(ggplot2)

oaat_hash_positions <- function(bit) {
op_1 <- function(pos, step, bit) {
updated_pos <- outer(pos, c(10, 0), FUN = '+') %>% as.vector() %>% unique()
tibble(bit = bit, x = step - 1, y = pos, xend = step, yend = updated_pos) %>%
filter(y >= 0 & y < 32 & yend >= 0 & yend < 31)
}
op_2 <- function(pos, step, bit) {
updated_pos <- outer(pos, c(0, -6), FUN = '+') %>% as.vector() %>% unique()
tibble(bit = bit, x = step - 1, y = pos, xend = step, yend = updated_pos) %>%
filter(y >= 0 & y < 32 & yend >= 0 & yend < 31)
}
final_op_1 <- function(pos, step, bit) {
updated_pos <- outer(pos, c(0, 3), FUN = '+') %>% as.vector() %>% unique()
tibble(bit = bit, x = step - 1, y = pos, xend = step, yend = updated_pos) %>%
filter(y >= 0 & y < 32 & yend >= 0 & yend < 31)
}
final_op_2 <- function(pos, step, bit) {
updated_pos <- outer(pos, c(0, -11), FUN = '+') %>% as.vector() %>% unique()
tibble(bit = bit, x = step - 1, y = pos, xend = step, yend = updated_pos) %>%
filter(y >= 0 & y < 32 & yend >= 0 & yend < 31)
}
final_op_3 <- function(pos, step, bit) {
updated_pos <- outer(pos, c(0, 15), FUN = '+') %>% as.vector() %>% unique()
tibble(bit = bit, x = step - 1, y = pos, xend = step, yend = updated_pos) %>%
filter(y >= 0 & y < 32 & yend >= 0 & yend < 31)
}

operation_1 <- map(bit, ~ op_1(.x, step = 1, bit = bit)) %>% bind_rows()
operation_2 <- map(operation_1$yend, ~ op_2(.x, step = 2, bit = bit)) %>% bind_rows()
operation_3 <- map(operation_2$yend, ~ op_1(.x, step = 3, bit = bit)) %>% bind_rows()
operation_4 <- map(operation_3$yend, ~ op_2(.x, step = 4, bit = bit)) %>% bind_rows()
operation_5 <- map(operation_4$yend, ~ op_1(.x, step = 5, bit = bit)) %>% bind_rows()
operation_6 <- map(operation_5$yend, ~ op_2(.x, step = 6, bit = bit)) %>% bind_rows()
operation_7 <- map(operation_6$yend, ~ op_1(.x, step = 7, bit = bit)) %>% bind_rows()
operation_8 <- map(operation_7$yend, ~ op_2(.x, step = 8, bit = bit)) %>% bind_rows()

operation_9 <- map(operation_8$yend, ~ final_op_1(.x, step = 9, bit = bit)) %>% bind_rows()
operation_10 <- map(operation_9$yend, ~ final_op_2(.x, step = 10, bit = bit)) %>% bind_rows()
operation_11 <- map(operation_10$yend, ~ final_op_3(.x, step = 11, bit = bit)) %>% bind_rows()

rbind(operation_1, operation_2,
operation_3, operation_4,
operation_5, operation_6,
operation_7, operation_8,
operation_9, operation_10, operation_11)
}
bit_movement <- do.call(rbind, lapply(0:31, oaat_hash_positions))

plot_bitmovement <- function(bm_segs) {
ggplot(bm_segs, aes(
x = x,
y = y,
xend = xend,
yend = yend)
) +
geom_segment(colour = "grey") +
geom_point(colour = "grey") +
geom_point(aes(x = xend, y = yend), colour = "grey") +
coord_flip() +
scale_y_reverse(breaks = 0:31, labels = 1:32) +
scale_x_reverse() +
theme_minimal() +
theme(
legend.position = "none"
) + ylab("Bit-position") + xlab("Operation")
}

plot_bitmovement(bit_movement)

最佳答案

您可以添加一个没有填充的 geom_rect 以在这些点周围放置边框。我不熟悉您的工作环境,因此您可能需要在此处进行一些调整。我向该函数添加了参数,因此您可以提供操作值向量和要突出显示的位置,以及要在框周围放置多少填充的值。

我没有解决你的中断和标签被 1 偏移的事实——就像我说的,这可能是你想要修复的特定于上下文的问题。

library(tidyverse)

# omitting function that creates bitmovement data

plot_bitmovement <- function(bm_segs, hilite_op, hilite_pos, box_pad = 0.4) {
hilite <- tibble(x = hilite_op, ymin = min(hilite_pos), ymax = max(hilite_pos))

ggplot(bm_segs, aes(
x = x,
y = y,
xend = xend,
yend = yend)
) +
geom_segment(colour = "grey") +
geom_point(colour = "grey") +
geom_point(aes(x = xend, y = yend), colour = "grey") +
# add hilite box #########
geom_rect(aes(xmin = x - box_pad,
xmax = x + box_pad,
ymin = ymin - box_pad,
ymax = ymax + box_pad),
data = hilite, color = "black", fill = "transparent", inherit.aes = F) +
##########################
coord_flip() +
scale_y_reverse(breaks = 0:31, labels = 1:32) +
scale_x_reverse() +
theme_minimal() +
theme(
legend.position = "none"
) + ylab("Bit-position") + xlab("Operation")
}

hilite_x <- seq(0, 6, by = 2)
hilite_y <- 1:8

plot_bitmovement(bit_movement, hilite_x, hilite_y)

reprex package 创建于 2018-06-09 (v0.2.0).

关于r - 通过将点包装在一个框中来注释 ggplot2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50775627/

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