Build Luv colors; replicating the 6.11 figure from the ggplot2 book
I'm trying to reproduce Figure 6.11 from Hadley Wickham's ggplot2 book , which represents R colors in Luv space; the colors of the points represent themselves and no legend is required.
Here are two attempts:
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")
Second try:
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")
There are several obvious problems:
- These charts do not look like shapes. Any suggestions on code needed?
- The first attempt generates many NA fields in Luv (only ~ 3100 named colors out of 10,000 runs versus ~ 9950 in the second run). If L is assumed to be 0-100 and u and v are between -100 and 100, why do I have so many neural networks in my first run? I've tried rounding, it doesn't help.
- Why do I have a legend?
Many thanks.
+3
source to share
1 answer
You get weird colors because it aes(color = Luv)
says, "Assign a color to every unique row in a column Luv
." If you assign color
out aes
, as shown below, it means "use these explicit colors". I think something like this should be close to the shape you presented.
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)
+5
source to share