How to change fill color in each facet using ggplot2?

0 votes

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,130 points
3,271 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,020 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 darklord
• 6,170 points
164 views
0 votes
2 answers
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
38 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 5 in Data Analytics by anonymous
3,519 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,702 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,459 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,130 points
187 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer