Monday, January 06, 2014

Pretty colors

Black-crested Titmouse
(Baeolophus atricristatus)
hybrid index=6
Tufted Titmouse
(Baeolophus bicolor)
hybrid index=0
I wanted a grayscale color ramp to use in my figures to color symbols to match the hybrid index used for my two study species.  This gray ramp should end up having black for Black-crested Titmouse (hybrid index of 6) and white for Tufted (hybrid index of 0), with hybrid index values 1-5 represented as shades of gray.

First, in case you want to go back to the way things were before, set the name of your default colors before you've changed them.

(default.colors<-palette()) #saves the color scheme you already have.
#The parentheses around it also prints it so you can see what's already happening.

#[1] "black"   "red"     "green3"  "blue"    "cyan"    "magenta" "yellow"  "gray"

#Now it's safe to change to what you want.  You can always go back if needed now.

#This function, gray.colors, asks for
#([number of colors], start=[start of gray ramp], end=[ending value of gray ramp],
#alpha=[optional opacity/transparency value, which I did not use in this example]).
#The gray ramp start and end values must be between 0 (black) and 1 (white).
#Here I had mine start with white (because I wanted "1" to equal "white"
#and "7" to equal "black" in the finished ramp.
#Essentially, I've asked for a seven-unit gray scale going from white to black.

(gray<-gray.colors(7, start=1, end=0))

#[1] "#FFFFFF" "#EBEBEB" "#D4D4D4" "#BABABA" "#9B9B9B" "#717171" "#000000"
#These are in hex codes, though some will show up as descriptions later.

#the argument to palette() is a value.  We have used "gray" that we created above.

#Since the hybrid index goes from 0 to 6, if I use the palette without adding 1 to my index values,
#it will put 0 as no color and no bird will get 7 (black).
#This is easily solved by pretending the hybrid index values are 1 through 7.
#In my example, this can be done by indexsum+1 when I'm inputting color options in the plotting function.
#You can make whatever adjustments are needed to your variable that specifies color
#To get it to match the ramp.  Copy your color variable first and make changes on the copy.

palette()#shows your current palette (either description or hex codes).

#[1] "white"   "gray92"  "gray83"  "gray73"  "#9B9B9B" "#717171" "black"

#I believe it changes the hex codes to named colors where available.
#It will be used until you say otherwise.
#If you want to go back to the original color palette after this section of code is over,
#just use your default that you saved at the start.

palette(default.colors) #return to original color scheme.  You can check this using...
palette() #See, it works!

No comments:

Post a Comment

Comments and suggestions welcome.