change the color for bars in geom bar in ggplot

+1 vote
Hi, my problem is i want to fill bars in differnt colors, but if i use color or fill it gives blue scale in bars. How to provide different colors or default colors for each bar?
Jul 23, 2019 in Data Analytics by radha
37,104 views

2 answers to this question.

+1 vote

Hi @radha,

You can have 2 types of a bar chart in ggplot ( Continuos or discrete )

For Continuous, You have 2 types of color scales, default and Viridis.

ggplot(mpg, aes(displ, hwy, fill = displ)) + geom_bar(stat = "identity")
ggplot(mpg, aes(displ, hwy, fill = displ)) + geom_bar(stat = "identity") + scale_fill_continuous(type = "viridis")

You can see below colors,

  

     

For discrete bars, you can provide colors or default colors by R.

You can change the palette using scale_colour_brewer(palette = "Set1,2, ... ") to chnage the colors.

Alternative using scale_fill_manual(values = c("red","blue","green","yellow"))

answered Jul 24, 2019 by Cherukuri
• 33,050 points
0 votes

Hi,

You can use the geom_bar() function to change the colors of bars as shown below.

df <- data.frame(dose=c("D0.5", "D1", "D2"), len=c(4.2, 10, 29.5)) 
head(df)
library(ggplot2) 
# Basic barplot 
p<-ggplot(data=df, aes(x=dose, y=len)) + geom_bar(stat="identity")
answered Dec 15, 2020 by MD
• 95,460 points

Related Questions In Data Analytics

0 votes
1 answer

How do you have 2 different colors in a bar graph, ggplot in where one color for the negatives and positives

Hi@Andris, Once you have the data, you can make the ...READ MORE

answered Oct 5, 2020 in Data Analytics by MD
• 95,460 points
1,720 views
0 votes
1 answer

How to find out the sum/mean for multiple variables per group in R?

You can use the reshape2 package for ...READ MORE

answered Apr 12, 2018 in Data Analytics by DataKing99
• 8,250 points
4,584 views
0 votes
2 answers

R function for finding the index of an element in a vector?

The function match works on vectors : x <- sample(1:10) x # ...READ MORE

answered Dec 12, 2020 in Data Analytics by Rajiv
• 8,870 points
57,967 views