Explore Courses Blog Tutorials Interview Questions
0 votes
in R Programming by (50.2k points)

I am having the factor has 2 levels that is A and B

data_set %>% 

    ggplot(aes(x = effort, y = rating, fill = factorAB)) + 

    geom_bar(position = "dodge", stat = "summary", fun.y = "mean") + 

    scale_fill_manual(name = "factorAB", values=c("darkgoldenrod2", "chartreuse4")) 

In the above code the Scale_fill_manual function assigns color to each factor, 1 color for A and 1 color for B, but I want to assign 4 different colors so that the effort 1 A is different from effort 2 A.

How can I achieve that?

1 Answer

0 votes
by (108k points)

You have written the syntax correctly but to achieve your goal you have to change the fill aesthetic to an interaction:

 ggplot(aes(x = effort, y = rating, fill = interaction(factorAB,effort)))

+ ...

If you want to know more about R then do check out the R programming tutorial that will help you in understanding R from scratch. 

Browse Categories