首页 > 解决方案 > 如何在R中制作和填充矩形?

问题描述

我想制作一个 8*10 的图,填充随机颜色,例如: 在此处输入图像描述

我知道rect(),但不知道该怎么做。

标签: rplot

解决方案


这是一个基本的 R 解决方案

rand_rgb <- function(n) rgb(runif(n), runif(n), runif(n))

w <- 10; h <- 8
# We can get a rectangular plot by simply resizing the canvas
png("test.png", width = 600, height = 200)
# set plot margins
par(mar = c(1, 1, 1, 1))
image(
  1:w, 1:h, matrix(runif(h * w), w, h), col = rand_rgb(h * w), 
  xlab = "", ylab = "", axes = FALSE
)
grid(w, h, lty = 1, col = "black")
box()
dev.off()

然后,您将看到一个名为“test.png”的图像出现在您的工作目录中。看起来像这样

测试


推荐阅读