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
35,817 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,030 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,113 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
3,861 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
56,681 views
0 votes
1 answer

What is the standard naming convention for the variables in R?

Use of period separator e.g. product.prices <- c(12.01, ...READ MORE

answered Apr 25, 2018 in Data Analytics by shams
• 3,670 points
670 views
0 votes
1 answer

How to plot side-by-side Plots with ggplot2 in R?

By Using gridExtra library we can easily ...READ MORE

answered Apr 16, 2018 in Data Analytics by DeepCoder786
• 1,720 points

edited Jun 9, 2020 by MD 8,874 views
0 votes
1 answer
0 votes
0 answers

Chart/plot to show 3 dimensions in a chart - R

Hi, I want to show chart with ...READ MORE

Jul 12, 2019 in Data Analytics by kalyan
553 views
0 votes
1 answer

Create a dual axis column/bar chart using ggplot in R

Add position = "stack" in geom_bar() function ...READ MORE

answered Nov 4, 2019 in Data Analytics by sindhu
4,671 views
0 votes
1 answer

Change the order of multiple legends in ggplot

Hi Radha, You can change the order of ...READ MORE

answered Jul 24, 2019 in Data Analytics by Cherukuri
• 33,030 points
25,149 views
0 votes
11 answers

Changing the legend title in ggplot

Hi, you can also try guides() to ...READ MORE

answered Jul 30, 2019 in Data Analytics by Cherukuri
• 33,030 points
17,729 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP