How to Manually Set Geom_Bar Fill Color in Ggplot

How to manually change the fill color of barplot when stat='count' in ggplot2

Don't use two scale_.*. You can add labels in scale_fill_manual itself.

library(ggplot2)

ggplot(data=mydata, aes(x=doctor,fill=compliance)) +
geom_bar(stat="count",width=0.4) +
geom_text(stat='count',aes(label=..count..), vjust=-0.3) +
expand_limits(y = c(0, 3)) +
labs(y = NULL, x= NULL) +
theme(legend.title = element_blank()) +
scale_fill_manual(values = c("#00BFC4","#F8766D"), labels = c("Not found","Compliance"))

Sample Image

Change bar plot colour in geom_bar with ggplot2 in r

If you want all the bars to get the same color (fill), you can easily add it inside geom_bar.

ggplot(data=df, aes(x=c1+c2/2, y=c3)) + 
geom_bar(stat="identity", width=c2, fill = "#FF6666")

Sample Image

Add fill = the_name_of_your_var inside aes to change the colors depending of the variable :

c4 = c("A", "B", "C")
df = cbind(df, c4)
ggplot(data=df, aes(x=c1+c2/2, y=c3, fill = c4)) +
geom_bar(stat="identity", width=c2)

Sample Image

Use scale_fill_manual() if you want to manually the change of colors.

ggplot(data=df, aes(x=c1+c2/2, y=c3, fill = c4)) + 
geom_bar(stat="identity", width=c2) +
scale_fill_manual("legend", values = c("A" = "black", "B" = "orange", "C" = "blue"))

Sample Image

How to manually set colors in a bar chart

you can use scale_fill_manual to control the colours.

So your call to ggplot becomes

ggplot(data=data, aes( x = Time,  y=value, fill=variable))   +  
geom_bar(stat="identity") +
ggtitle("% ") + xlab("Time") +
ylab("Percentage") +theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
scale_fill_manual(values=c("purple","black","red"))

How to use scale_fill_manual to manually define bar plot colors

I would create a dummy variable in your input df2 for the ggplot fill aesthetic. Here, "-999" refers to "OJ" and will be coloured in black.

I've also updated your myPalette to include black in it, and also setNames to it so that only "-999" will have a corresponding "black" value.

library(tidyverse)

df2 <- data.frame(supp=rep(c("VC", "OJ"), each=3),
dose=rep(c("D0.5", "D1", "D2"),2),
len=c(6.8, 15, 33, 4.2, 10, 29.5))

myPalette <- c("#05eb92", "#119da4", "#ffc857")

myPalette_with_black <- setNames(c(myPalette, "black"),
c(as.character(seq_along(myPalette)), "-999"))

df2 <- df2 %>%
group_by(supp) %>%
mutate(dummy = ifelse(supp == "VC", as.character(row_number()), "-999"))

ggplot(data=df2, aes(x=dose, y=len, fill = dummy)) +
geom_bar(stat="identity", position=position_dodge()) +
scale_fill_manual(values = myPalette_with_black) +
theme(legend.position = "none")

Sample Image

Created on 2022-03-12 by the reprex package (v2.0.1)

How do you order the fill-colours within ggplot2 geom_bar

You need to specify the order aesthetic as well.

ggplot(data,aes(mon,NG,fill=gclass,order=gclass))+
geom_bar(stat="identity")

Sample Image

This may or may not be a bug.

ggplot2: geom_bar fill colour; how to change to different grouping of data

You can set the fill variable as product as that defines the color you want and specify an additional group variable as FISCAL_PERIOD so that each bar will still be segmented by year:

ggplot(data = app, aes(x = Product, y = sum_trans, fill = Product, group = FISCAL_PERIOD)) + 
geom_bar(stat="identity", position="dodge", colour="black")

Sample Image

ggplot2: How do I set the default fill-colour of geom_bar() in a theme

you can't do it in a theme (sadly).

You want to change the default settings of a geom,

  update_geom_defaults("bar",   list(fill = "red"))

and you can also change a default scale, e.g.

  scale_colour_continuous <- function(...) 
scale_colour_gradient(low = "blue", high = "red", na.value="grey50", ...)


Related Topics



Leave a reply



Submit