Change color of only one bar in ggplot

I want to color only one bar in ggplot. This is my data frame:

area <- c("Północ", "Południe", "Wschód", "Zachód") sale <- c(16.5, 13.5, 14, 13) df.sale <- data.frame(area, sale) colnames(df.sale) <- c("Obszar sprzedaży", "Liczba sprzedanych produktów (w tys.)") 

And the code to build:

 plot.sale.bad <- ggplot(data=df.sale, aes(x=area, y=sale, fill=area)) + geom_bar(stat="identity") + scale_fill_manual(values=c("black", "red", "black", "black")) + xlab(colnames(df.sale)[1]) + ylab(colnames(df.sale)[2]) + ggtitle("Porównanie sprzedaży") 

I would like to have only one color in the bar and 3 other colors by default (darkgrey, not black, this looks bad for me). How to change the color only on the panel or how to get the default color name for bars to place them instead of black?

+6
source share
1 answer

Option 1: change the color of only one panel. Following Henrik’s suggestion, you can create a new variable with NA for standard colors and character strings / coefficients for colors other than the default values ​​(the first one is red):

 area.color <- c(NA, "withcolor", NA, NA) plot.sale.bad <- ggplot(data=df.sale, aes(x=area, y=sale, fill=area.color)) + geom_bar(stat="identity") + xlab(colnames(df.sale)[1]) + ylab(colnames(df.sale)[2]) + ggtitle("Porównanie sprzedaży") plot.sale.bad 

Option 2: Find the default name of the dark gray color that you like. This is not the default color if you just delete the scale_fill_manual line in the source code (in this case you get four different pastels), so I assume that you mean the gray color created by the code block just above this paragraph, for those bars where area.color==NA . In this case, you can look at the source code (or args, anyway) for scale_fill_discrete :

 > args(scale_fill_discrete) # function (..., h = c(0, 360) + 15, c = 100, l = 65, h.start = 0, # direction = 1, na.value = "grey50") # NULL 

The default for na.value is "grey50" . Therefore, if you want to use scale_fill_manual , you can do it like this:

 plot.sale.bad <- ggplot(data=df.sale, aes(x=area, y=sale, fill=area)) + geom_bar(stat="identity") + scale_fill_manual(values=c("grey50", "red", "grey50", "grey50")) + xlab(colnames(df.sale)[1]) + ylab(colnames(df.sale)[2]) + ggtitle("Porównanie sprzedaży") plot.sale.bad 
+7
source

All Articles