2013-02-12 59 views
3

我试图从Hadley Wickham的ggplot2 book复制图6.11,它绘制了Luv空间中的R颜色;点的颜色代表自己,没有传说是必要的。 enter image description here绘制Luv颜色;从ggplot2书复制图6.11

这里有两个企图:

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") 

enter image description here

第二次尝试:

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") 

enter image description here

有几个明显的问题s:

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

非常感谢。

回答

5

由于aes(color = Luv)表示“给列Luv中的每个唯一字符串分配一个颜色”,所以你会得到奇怪的颜色。如果您在aes之外分配color,如下所示,则表示“使用这些明确的颜色”。我认为像这样的东西应该接近你提供的数字。

require(colorspace) 
x <- sRGB(t(col2rgb(colors()))) 
storage.mode([email protected]) <- "numeric" # as(..., "LUV") doesn't like integers for some reason 
y <- as(x, "LUV") 
DF <- as.data.frame([email protected]) 
DF$col <- colors() 
ggplot(DF, aes(x = U, y = V)) + geom_point(colour = DF$col) 
+0

就是这样,谢谢! – koenbro 2013-02-12 07:29:49

+3

或更好,请使用'scale_colour_identity' – hadley 2013-02-12 13:54:15

+0

@hadley。我不知道该怎么做。 'ggplot(DF,aes(x = U,y = V,fill = colors()))+ geom_point()+ scale_color_identity()'不起作用 - 给出黑点和标签。你能否展示一个可重复使用的小例子?谢谢 – koenbro 2013-02-12 21:31:30