2016-02-23 4 views
5

ich die hexadezimalen Codes der Farben erhalten mag, dass die scale_fill_grey Funktion die Kategorien des BarPlot durch die folgenden Codes erzeugt zu füllen verwendet:Erhalten hexadezimalen Farbcodes verwendete in „scale_fill_grey“ -Funktion

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

+2

Erste Zeile in '? Scale_fill_grey':" Basierend auf 'grey.colors'" – Henrik

Antwort

6

scale_fill_grey() verwendet grey_pal() aus dem scales Paket, das seinerseits grey.colors() verwendet. So können Sie die Codes für die Skala von vier Farben erzeugen, die Sie wie folgt verwendet:

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

Dies zeigt ein Diagramm mit den Farben

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

enter image description here

2

Mit ggplot_build() Funktion:

#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 x  y 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 
Verwandte Themen