change the color for bars in geom_bar in ggplot

0 votes
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
13,318 views

1 answer to this question.

0 votes

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
• 32,490 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 in Data Analytics by MD
• 66,940 points
118 views
0 votes
1 answer

What are the options for deploying models in production with R?

Well, I could say that the answer ...READ MORE

answered Apr 12, 2018 in Data Analytics by DataKing99
• 8,210 points
645 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,210 points
1,555 views
0 votes
1 answer

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

Yes, we can find the index of ...READ MORE

answered Apr 13, 2018 in Data Analytics by zombie
• 3,750 points
24,557 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 by MD 4,516 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
98 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
691 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
• 32,490 points
4,565 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
• 32,490 points
12,200 views