2013-02-12 23 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