3

我试图从 Hadley Wickham 的ggplot2 书中复制图 6.11 ,它在 Luv 空间中绘制 R 颜色;点的颜色代表自己,不需要图例。 在此处输入图像描述

这里有两个尝试:

library(colorspace)
myColors <- data.frame("L"=runif(10000, 0,100),"a"=runif(10000, -100, 100),"b"=runif(10000, -100, 100))
myColors <- within(myColors, Luv <- hex(LUV(L, a, b)))
myColors <- na.omit(myColors)
g <- ggplot(myColors, aes(a, b, color=Luv), size=2)
g + geom_point() + ggtitle ("mycolors")

在此处输入图像描述

第二次尝试:

other <- data.frame("L"=runif(10000),"a"=runif(10000),"b"=runif(10000))
other <- within(other, Luv <- hex(LUV(L, a, b)))
other <- na.omit(other)
g <- ggplot(other, aes(a, b, color=Luv), size=2)
g + geom_point() + ggtitle("other")

在此处输入图像描述

有几个明显的问题:

  1. 这些图表看起来与该图完全不同。关于所需代码的任何建议?
  2. 第一次尝试在 Luv 列中生成了很多 NA 字段(在 10,000 次运行中只有约 3100 种命名颜色,而第二次运行中约 9950 种)。如果 L 应该在 0-100 之间,而 u 和 v 在 -100 和 100 之间,为什么我在第一次运行时会有这么多 NA?我试过四舍五入,它没有帮助。
  3. 为什么我有传奇?

非常感谢。

4

1 回答 1

5

你会得到奇怪的颜色,因为aes(color = Luv)说“为列中的每个唯一字符串分配一种颜色Luv”。如果您color在 之外分配aes,如下所示,这意味着“使用这些显式颜色”。我认为这样的事情应该接近你提出的数字。

require(colorspace)
x <- sRGB(t(col2rgb(colors())))
storage.mode(x@coords) <- "numeric" # as(..., "LUV") doesn't like integers for some reason
y <- as(x, "LUV")
DF <- as.data.frame(y@coords)
DF$col <- colors()
ggplot(DF, aes( x = U, y = V)) + geom_point(colour = DF$col)
于 2013-02-12T07:11:04.003 回答