I'm reading the book "ggplot2 - Elegant Graphics for Data Analysis" (Wickham, 2009), the section "Scaling" (page 32) says this:
Scaling then involves mapping the data values to points in this space.
There are many ways to do this, but here since cyl is a categorical
variable we map values to evenly spaced hues on the colour wheel, as
shown in Figure 3.4. A different mapping is used when the variable is
continuous. The result of these conversions is Table 3.4, which
contains values that have meaning to the computer.
![](https://www.manongdao.com/static/images/pcload.jpg)
![](https://www.manongdao.com/static/images/pcload.jpg)
The book doesn't explain in detail how to get this table 3.4, much less figure 3.4. The built-in database is mpg. Anyone has an idea how to get this table and graph?
Thanks in advance.
Was wondering how to do this without coord_polar()
, since the example from Wickham's book clearly does not. Turns out you can just use geom_point(...)
.
library(ggplot2)
r <- seq(0,1,length=201)
th <- seq(0,2*pi, length=201)
d <- expand.grid(r=r,th=th)
gg <- with(d,data.frame(d,x=r*sin(th),y=r*cos(th),
z=hcl(h=360*th/(2*pi),c=100*r, l=65)))
ggplot(gg) +
geom_point(aes(x,y, color=z), size=3)+
scale_color_identity()+labs(x="",y="") +
coord_fixed()
![](https://www.manongdao.com/static/images/pcload.jpg)
This renders in a few seconds. This reference states that the default luminance, l=65.
This is close to what you're looking for, but the color transitions might not be smooth enough. Hopefully someone else can improve upon this:
Code adapted from here.
# Create hsv grid
d = expand.grid(h=seq(0,1,0.01), s=seq(0,1,0.05), v=1)
p1 = ggplot() +
coord_polar(theta="x") +
scale_x_continuous(breaks=NULL) +
scale_y_continuous(breaks=NULL) +
scale_fill_identity() +
geom_rect(data=d, mapping=aes(xmin=h, xmax=h+resolution(h),
ymin=s, ymax=s+resolution(s),
fill=hsv(h,s,v)))
You can get smoother color transitions by using a finer grid for the h
and s
values, but it takes a long time to render the plot. You can change the brightness by setting the value of v
between zero and 1. (Per @BrodieG's comment, set v=1/2 to get the brightness level of the figure in Hadley's ggplot2 book.)
Below is a version of the plot using a step-size of 0.001 for the h
values (while keeping the s
step-size at 0.05). This took a couple of minutes to render on my relatively new Macbook Pro, but the transitions are pretty smooth along the h
coordinate:
png("Colour wheel.png", 2000, 2000)
p1
dev.off()
![](https://www.manongdao.com/static/images/pcload.jpg)
To get a table of colors, you can use the hsv()
function, which returns hexadecimal color values. For example:
# Make up some hsv colors
colors = data.frame(h=seq(0.1,0.5,length.out=6),
s=seq(0.5,0.9,length.out=6),
v=c(.5,.5,.5,.9,.9,.9))
# Convert to hexadecimal
apply(colors, 1, function(x) hsv(x[1],x[2],x[3]))
[1] "#806640" "#7A8036" "#50802B" "#3CE642" "#29E68B" "#17E6E6"
# Plot them to see what they look like
plot(1:6,rep(1,6), pch=15, cex=5, col=apply(colors, 1, function(x) hsv(x[1],x[2],x[3])))