绘制Luv颜色; 从ggplot2书中复制图6.11

koe*_*bro 3 r colors color-space ggplot2

我试图从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")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

第二次尝试:

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")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

有几个明显的问题:

  1. 这些图形看起来不像图.有关代码的任何建议吗?
  2. 第一次尝试在Luv列中生成了大量的NA字段(在10,000次运行中只有~3100种命名颜色,而在第二次运行中则为~9950).如果L应该介于0-100和u和v介于-100和100之间,为什么我在第一次运行时有这么多的NA?我试过四舍五入,但没有用.
  3. 为什么我有一个传奇?

非常感谢.

use*_*457 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)
Run Code Online (Sandbox Code Playgroud)

  • 或者更好,使用`scale_colour_identity` (3认同)