根据高斯分布绘制彩色像素的正方形
Plotting squares of colored pixels from the Gaussian distribution
我想生成这样的图像:
其中像素颜色由多元正态分布的值决定。换句话说,我有以下数据框:
images <- data.frame(x = rnorm(256*256*20), image = rep(c(1:20), each = 256*256))
我想将多元法线的 20 个绘图向量中的每一个绘制成彩色方块。正方形应在 2x10 网格上对齐。它们是否被黑线分隔并不重要,但不同方块之间必须有某种分隔。值 0 应对应于浅灰色或白色(灰色会更好)。我怎样才能做到这一点?我想要一个 ggplot
解决方案,但基础 R 或其他东西也很好,只要代码相当灵活和可读(性能不是问题)。
编辑 方块不必是 256*256,64*64 也可以。
这是一次尝试:
生成 r、g、b 通道:
library(tidyverse)
images <- data.frame(r = rnorm(256*256*20),
g = rnorm(256*256*20),
b = rnorm(256*256*20),
image = rep(c(1:20), each = 256*256))
将 rgb 转换为十六进制的函数:
rgb2hex <- function(r,g,b) rgb(r, g, b, maxColorValue = 255)
通过重新缩放到 0-1 范围并乘以 255 最后转换为十六进制,将具有 0 均值和 1 sd 的 rnorm
转换为 r、g、b 通道
images %>%
group_by(image) %>%
mutate(y = rep(1:256, each = 256), #x coords
x = rep(1:256, times = 256), #y coords
r = round(scales::rescale(r) * 255, 0),
g = round(scales::rescale(g) * 255, 0),
b = round(scales::rescale(b) * 255, 0),
hex = rgb2hex(r, g, b)) -> for_plot
ggplot(for_plot) +
geom_raster(aes(x = x, y = y), fill = for_plot$hex)+
facet_wrap(~image, ncol = 10) +
coord_equal()
简化:
rgb2hex <- function(r,g,b) rgb(r, g, b, maxColorValue = 1)
images %>%
group_by(image) %>%
mutate(y = rep(1:256, each = 256),
x = rep(1:256, times = 256),
r = scales::rescale(r),
g = scales::rescale(g),
b = scales::rescale(b),
hex = rgb2hex(r, g, b)) -> for_plot
编辑:DeltaIV 要求在评论中剪掉较暗的色调是一种方法:
n = 0.2
images %>%
group_by(image) %>%
mutate(y = rep(1:256, each = 256),
x = rep(1:256, times = 256),
r = scales::rescale(r) + n,
g = scales::rescale(g) + n,
b = scales::rescale(b) + n,
hex = rgb(r, g, b, maxColorValue = 1 + n)) -> for_plot
ggplot(for_plot) +
geom_raster(aes(x = x, y = y), fill = for_plot$hex)+
facet_wrap(~image, ncol = 10) +
coord_equal()
增加 n = 0.5 会使图像更亮
我想生成这样的图像:
其中像素颜色由多元正态分布的值决定。换句话说,我有以下数据框:
images <- data.frame(x = rnorm(256*256*20), image = rep(c(1:20), each = 256*256))
我想将多元法线的 20 个绘图向量中的每一个绘制成彩色方块。正方形应在 2x10 网格上对齐。它们是否被黑线分隔并不重要,但不同方块之间必须有某种分隔。值 0 应对应于浅灰色或白色(灰色会更好)。我怎样才能做到这一点?我想要一个 ggplot
解决方案,但基础 R 或其他东西也很好,只要代码相当灵活和可读(性能不是问题)。
编辑 方块不必是 256*256,64*64 也可以。
这是一次尝试:
生成 r、g、b 通道:
library(tidyverse)
images <- data.frame(r = rnorm(256*256*20),
g = rnorm(256*256*20),
b = rnorm(256*256*20),
image = rep(c(1:20), each = 256*256))
将 rgb 转换为十六进制的函数:
rgb2hex <- function(r,g,b) rgb(r, g, b, maxColorValue = 255)
通过重新缩放到 0-1 范围并乘以 255 最后转换为十六进制,将具有 0 均值和 1 sd 的 rnorm
转换为 r、g、b 通道
images %>%
group_by(image) %>%
mutate(y = rep(1:256, each = 256), #x coords
x = rep(1:256, times = 256), #y coords
r = round(scales::rescale(r) * 255, 0),
g = round(scales::rescale(g) * 255, 0),
b = round(scales::rescale(b) * 255, 0),
hex = rgb2hex(r, g, b)) -> for_plot
ggplot(for_plot) +
geom_raster(aes(x = x, y = y), fill = for_plot$hex)+
facet_wrap(~image, ncol = 10) +
coord_equal()
简化:
rgb2hex <- function(r,g,b) rgb(r, g, b, maxColorValue = 1)
images %>%
group_by(image) %>%
mutate(y = rep(1:256, each = 256),
x = rep(1:256, times = 256),
r = scales::rescale(r),
g = scales::rescale(g),
b = scales::rescale(b),
hex = rgb2hex(r, g, b)) -> for_plot
编辑:DeltaIV 要求在评论中剪掉较暗的色调是一种方法:
n = 0.2
images %>%
group_by(image) %>%
mutate(y = rep(1:256, each = 256),
x = rep(1:256, times = 256),
r = scales::rescale(r) + n,
g = scales::rescale(g) + n,
b = scales::rescale(b) + n,
hex = rgb(r, g, b, maxColorValue = 1 + n)) -> for_plot
ggplot(for_plot) +
geom_raster(aes(x = x, y = y), fill = for_plot$hex)+
facet_wrap(~image, ncol = 10) +
coord_equal()
增加 n = 0.5 会使图像更亮