Get the hexadecimal color codes used in the scale_fill_grey function

I want to get the hexadecimal color codes that the scale_fill_grey function scale_fill_grey to populate the categories of barcode generated 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

+5
source share
2 answers

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

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

Here is a graph with flowers

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

enter image description here

+6
source

Using the ggplot_build() function:

 #assign ggplot to a variable myplot <- ggplot(data=data, aes(x=Meal, y=Cost, fill=Meal)) + geom_bar(stat="identity") + scale_fill_grey(start=0.8, end=0.2) #get build myplotBuild <- ggplot_build(myplot) #see colours myplotBuild$data # [[1]] # fill xy PANEL group ymin ymax xmin xmax colour size linetype alpha # 1 #CCCCCC 1 9.75 1 1 0 9.75 0.55 1.45 NA 0.5 1 NA # 2 #ABABAB 2 13.00 1 2 0 13.00 1.55 2.45 NA 0.5 1 NA # 3 #818181 3 19.00 1 3 0 19.00 2.55 3.45 NA 0.5 1 NA # 4 #333333 4 10.20 1 4 0 10.20 3.55 4.45 NA 0.5 1 NA 
+2
source

Source: https://habr.com/ru/post/1243701/


All Articles