Hey everyone, I'm taking the Google Data Analytics course on Coursera, and I wanted to know how to incorporate an if_else statement into my code to change the colors of charts based on conditions. The example the course showed me was a little confusing because they showed me on line of code, but I would've liked for them to show me all the code they used to make the graph, and where that specific if_else condition would fit into the entire code. So could someone give me an idea of where this piece of code would fit into with any kind of graph? Thank you.
scale_color_identity() allows color names that appear inside an aes() call to be interpreted literally. (They're automatically interpreted literally when they're not inside an aes() call, as in this example.)
Yes, scale_fill_identity() is different from scale_fill_manual().
If I have an aes() where I say fill = variable_name, and the variable has the value "blue" then scale_fill_identity will give you the colour blue/ Scale fill_manual will give you whatever you tell it to.
For example:
would render blue as chcoolate and yellow as green, but show the -names- of those colors in the legend as "blue" and "yellow". scales map a value to another value, scale_*_identity maps to the colour with the same name, because it doesn't need to be scaled, we already know what the colour are. Of course this only works if the names exists as a colour:
fill = ifelse(y < 0.5, "rosebud", "yellow")
would give an error:
Error in `geom_col()`:
! Problem while converting geom to grob.
ℹ Error occurred in the 1st layer.
Caused by error:
! Unknown colour name: rosebud