Ggplot2: How to Set the Default Fill-Colour of Geom_Bar() in a Theme

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", ...)

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

Change ggplot bar chart fill colors

It does not look like this is supported natively in ggplot. I was able to get something close by adding additional rows, ranging from 0 to value) to the data. Then use geom_tile and separating the tiles by specifying width.

library(tidyverse)

df <- data.frame(value = c(20, 50, 90),
group = c(1, 2, 3))

df_expanded <- df %>%
rowwise() %>%
summarise(group = group,
value = list(0:value)) %>%
unnest(cols = value)

df_expanded %>%
ggplot() +
geom_tile(aes(
x = group,
y = value,
fill = value,
width = 0.9
)) +
coord_flip() +
scale_fill_viridis_c(option = "C") +
theme(legend.position = "none")

Sample Image

If this is too pixilated you can increase the number of rows generated by replacing list(0:value) with seq(0, value, by = 0.1).

Sample Image

Use color names specified in data as fill color in geom_bar

If you wish to use the raw values, without scaling, for your aesthetics, then scale_identity can be used. Using "DF" from @Sandy Muspratt's answer:

ggplot(DF, aes(x = INTERVAL, y = HOURS, fill = BARCOLOR))+
geom_bar(stat = "identity")+
theme(legend.position = "none") +
scale_fill_identity()

Sample Image

Integrate default color into personalized theme ggplot

The following worked for me. theme_uwv2 needed the value returned from theme_uwv() as a list element, not the function itself. Also, you were making a plot where the fill was the dominant colour variable, so I've substituted scale_color_manual() with scale_fill_manual() for demonstration purposes.

library(ggplot2)
library(ggthemes)

df <- mtcars
uwvPalet <- c("#0078D2","#003282","#C4D600")
theme_uwv <- function(base_size = 22, base_family = "Verdana"){
theme_hc(base_size = base_size, base_family = base_family) %+replace%
theme(plot.title = element_text(color = rgb(0, 120, 210, maxColorValue = 255)),
complete = TRUE)}
theme_uwv2 <- list(theme_uwv(), scale_fill_manual(values = uwvPalet))

ggplot(df, aes(fill = as.factor(cyl), x = am, y = mpg)) +
geom_col(position = "dodge") +
ggtitle("test") +
theme_uwv2

Sample Image



Related Topics



Leave a reply



Submit