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
19,257 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
• 32,910 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,040 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,040 points
203 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,240 points
750 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,240 points
1,939 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 11, 2020 in Data Analytics by Rajiv
• 8,880 points
34,914 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 5,750 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
124 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
1,107 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,910 points
6,600 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,910 points
13,394 views