How to change fill color in each facet using ggplot2

+1 vote

I am trying to visualize data using ggplot2.

I have 3 facet figures with geombars in each

So, here each facet represents a particular variable that I have used in other graphs where ggplot2 assigns them to fill colors by default.

The faceted plots are black by default.

 I can change the fill color for all three panels but haven't found how to change each panel's fill to a different color.

Here is a sample data and code to represent the basic idea:

df = data.frame(matrix(data=c(1,3,1,2,3,1,2,3,2,3,1,2,3,1,2,3,
                              1,3,2,1,2,2,2,3,3,3,1,1,1,3,3,2), 
                       ncol=2,byrow=TRUE))
dimnames(df)[[2]] =c("x","y")

dodgebars <- 
  ggplot(data = df,aes(factor(y),fill=factor(x))) +
  geom_bar(aes(group=factor(x)),
           position="dodge")
facetedbars <- 
  ggplot(data = df, aes(factor(y))) +
  geom_bar(aes(group=x)) +
  facet_grid(~x)

How to match the color of each facet to fill its cover in "dodgebars"?

May 8, 2018 in Data Analytics by DataKing99
• 8,250 points
25,568 views

1 answer to this question.

0 votes

You can map the facetting variable to fill color.

 Then, every facet gets a different bar color:

facetedbars + aes(fill = as.factor(x))
answered May 8, 2018 by kappa3010
• 2,090 points

Related Questions In Data Analytics

0 votes
1 answer

How to change y axis max in time series using R?

The axis limits are being set using ...READ MORE

answered Apr 3, 2018 in Data Analytics by Sahiti
• 6,370 points
3,870 views
+1 vote
3 answers

How to change the value of a variable using R programming in a data frame?

Try this: df$symbol <- as.character(df$symbol) df$symbol[df$sym ...READ MORE

answered Jan 11, 2019 in Data Analytics by Tyrion anex
• 8,700 points
35,911 views
0 votes
1 answer

How to change mulitiple characters in a column to a date

Firstly we have to set dataf variable ...READ MORE

answered Apr 3, 2018 in Data Analytics by DeepCoder786
• 1,720 points
826 views
0 votes
2 answers

How to use group by for multiple columns in dplyr, using string vector input in R?

data = data.frame(   zzz11def = sample(LETTERS[1:3], 100, replace=TRUE),   zbc123qws1 ...READ MORE

answered Aug 6, 2019 in Data Analytics by anonymous
14,140 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,897 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,785 views
0 votes
1 answer

How to order bars in a bar graph using ggplot2?

The key to ordering is to set ...READ MORE

answered Jun 1, 2018 in Data Analytics by DataKing99
• 8,250 points
1,230 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
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