Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot a colour wheel by using ggplot?

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.

enter image description here

enter image description here

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.

like image 476
Ουιλιαμ Αρκευα Avatar asked Jan 31 '14 21:01

Ουιλιαμ Αρκευα


People also ask

How do I specify colors in ggplot2?

The colors of lines and points can be set directly using colour="red" , replacing “red” with a color name. The colors of filled objects, like bars, can be set using fill="red" .

What colors are available in Ggplot?

rgb() → The rgb() function allows to build a color using a quantity of red, green and blue. An additionnal parameter is available to set the transparency. All parameters ranged from 0 to 1.

What is the difference between Ggplot and plot?

Base R plots two vectors as x and y axes and allows modifications to that representation of data whereas ggplot2 derives graphics directly from the dataset. This allows faster fine-tuning of visualizations of data rather than representations of data stitched together in the Base R package1.

How do I assign a color to a variable in R?

In R, colors can be specified either by name (e.g col = “red”) or as a hexadecimal RGB triplet (such as col = “#FFCC00”). You can also use other color systems such as ones taken from the RColorBrewer package.


2 Answers

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

This renders in a few seconds. This reference states that the default luminance, l=65.

like image 62
jlhoward Avatar answered Sep 27 '22 19:09

jlhoward


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

enter image description here

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])))
like image 28
eipi10 Avatar answered Sep 27 '22 17:09

eipi10