Last active
May 14, 2021 10:44
-
-
Save mmparker/6315074 to your computer and use it in GitHub Desktop.
An example of using a named color vector to set colors in ggplot2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # The incantation | |
| options(stringsAsFactors = FALSE) | |
| library(ggplot2) | |
| # Some sample data | |
| # A test that can result in a negative or positive only | |
| testres2 <- data.frame(result = c("Negative", "Positive"), | |
| n = c(850, 150) | |
| ) | |
| # A different test that has a borderline result | |
| testres3 <- data.frame(result = c("Negative", "Positive", "Borderline"), | |
| n = c(700, 270, 30) | |
| ) | |
| # I want to use the same color scale for both tests, even though | |
| # they have different result sets | |
| # Red, green, blue | |
| resultcolors <- c("#1F78B4", "#F03B20", "#31A354") | |
| # In this case, ggplot2 just uses the first two colors in the vector | |
| ggplot(testres2, aes(x = result, weight = n, fill = result)) + | |
| geom_bar() + | |
| scale_fill_manual("Result", values = resultcolors) | |
| # Here it uses all three - assigning them to results in the | |
| # order they appear in the data | |
| ggplot(testres3, aes(x = result, weight = n, fill = result)) + | |
| geom_bar() + | |
| scale_fill_manual("Result", values = resultcolors) | |
| # So if I change the order the results appear in for whatever | |
| # reason, my color scheme is hosed | |
| ggplot(testres3[order(testres3$result), ], aes(x = result, weight = n, fill = result)) + | |
| geom_bar() + | |
| scale_fill_manual("Result", values = resultcolors) | |
| # I want negative to be red, positive to be blue, and | |
| # indeterminate to be green - everywhere, and without regard | |
| # to the order of the data.frame | |
| # Using a named vector instead... | |
| namedcolors <- resultcolors | |
| names(namedcolors) <- c("Positive", "Negative", "Borderline") | |
| # Even though only two colors are in use, this plot correctly uses | |
| # red and blue | |
| ggplot(testres2, aes(x = result, weight = n, fill = result)) + | |
| geom_bar() + | |
| scale_fill_manual("Result", values = namedcolors) | |
| # Same here | |
| ggplot(testres3, aes(x = result, weight = n, fill = result)) + | |
| geom_bar() + | |
| scale_fill_manual("Result", values = namedcolors) | |
| # Order of values in data doesn't matter | |
| ggplot(testres3[order(testres3$result), ], | |
| aes(x = result, weight = n, fill = result)) + | |
| geom_bar() + | |
| scale_fill_manual("Result", values = namedcolors) | |
| # Even if I put my thang down, flip it and reverse it | |
| ggplot(testres3[order(testres3$result, decreasing = TRUE), ], | |
| aes(x = result, weight = n, fill = result)) + | |
| geom_bar() + | |
| scale_fill_manual("Result", values = namedcolors) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This was the answer to something I have been trying to figure out for 2 hours. Thanks for this!