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,100 points
1,983 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,010 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,140 points
69 views
0 votes
1 answer

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$symb ...READ MORE

answered Jan 11 in Data Analytics by Tyrion anex
• 8,280 points
80 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,700 points
20 views
0 votes
1 answer

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

dplyr added versions for group_by. This allows you ...READ MORE

answered Apr 12, 2018 in Data Analytics by CodingByHeart77
• 3,680 points

edited Apr 12, 2018 by CodingByHeart77 2,239 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,700 points
1,172 views
0 votes
10 answers

Changing the legend title in ggplot

Example : p <- ggplot(mtcars, aes(mpg, wt, colour ...READ MORE

answered Dec 10, 2018 in Data Analytics by Rajni
4,329 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,100 points
107 views
0 votes
0 answers
0 votes
1 answer
0 votes
1 answer