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 in Data Analytics by radha
363 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 by Cherukuri
• 31,840 points

Related Questions In Data Analytics

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,130 points
297 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,130 points
425 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,690 points
741 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,580 points
33 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
1,703 views
0 votes
1 answer
0 votes
0 answers
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 in Data Analytics by sindhu
82 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 in Data Analytics by Cherukuri
• 31,840 points
289 views
0 votes
11 answers

Changing the legend title in ggplot

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

answered Jul 30 in Data Analytics by Cherukuri
• 31,840 points
6,466 views