Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtain hexadecimal color codes used in “scale_fill_grey” function

Tags:

r

ggplot2

I want to get the hexadecimal codes of the colors that the scale_fill_grey function uses to fill the categories of the barplot produced by the following codes:

library(ggplot2)

data <- data.frame(
  Meal = factor(c("Breakfast","Lunch","Dinner","Snacks"), 
  levels=c("Breakfast","Lunch","Dinner","Snacks")),
  Cost = c(9.75,13,19,10.20))

ggplot(data=data, aes(x=Meal, y=Cost, fill=Meal)) +
    geom_bar(stat="identity")  +
    scale_fill_grey(start=0.8, end=0.2)

enter image description here

like image 830
R. Joe Avatar asked Feb 23 '16 19:02

R. Joe


Video Answer


1 Answers

scale_fill_grey() uses grey_pal() from the scales package, which in turn uses grey.colors(). So, you can generate the codes for the scale of four colours that you used as follows:

grey.colors(4, start = 0.8, end = 0.2)
## [1] "#CCCCCC" "#ABABAB" "#818181" "#333333"

This shows a plot with the colours

image(1:4, 1, matrix(1:4), col = grey.colors(4, start = 0.8, end = 0.2))

enter image description here

like image 106
Stibu Avatar answered Sep 22 '22 01:09

Stibu