Dear All,
I am having trouble correctly specifying colors for bars and lines in ggplot output in markdown documents.
The colors that I specify by using Hex codes (“#E6AB02”,“#666666”) are not the colors that appear in the output ( “#D95F02”, “#1B9E77”). The undesired colors that do appear are from the correct palette (RColorBrewer Dark2), which is the palette that I specify and also contains the correct colors.
No matter which hex codes I specify in the aes() statement, the hex codes that are applied are the hex codes for the first two colors in the RColorBrewer Dark2 palette. The codes that I mean to specify are the 6th and 8th colors in that palette. I've included the palette and the hex codes in the output that is attached to this post.
I suspect this has something to do with the pallet but exactly what is wrong has me flummoxed.
I'd appreciate any insight to solve this problem.
Thanks.
Linda
Output: ggplot_specifying colors.pdf (92.3 KB)
---
title: "THE COLORS THAT ARE SPECIFIED ARE NOT THE COLORS THAT APPEAR"
author: "LandonPhD"
date: "`r format(Sys.time(), '%d %B %Y')`"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
# create data
Lender_vector <- c("JOHN DEERE INDL CREDIT", "C N H INDL CAPITAL AMER",
"KUBOTA CREDIT CORP USA",
"BANK STAR OF THE BOOTHEEL", "AGCO FIN",
"LEASE CNSLT CORP", "SOUTHERN MO BANK & TRUST",
"FARM CREDIT SVC", "1ST COMMUNITY BANK",
" 1ST NATL BANK", "1ST BANK",
"1ST STATE BANK & TRUST", "AGRICREDIT ACCEPT LLC",
"BANK OF MTN VIEW", "CITICAPITAL COMMERCIAL LSG",
"PROGRESSIVE FARM CREDIT SVC", "U S BANK")
Lender_percent_vector <- c(20, 19, 10, 8, 6, 6, 6, 5, 3, 3, 2, 2, 2, 2, 2, 2, 2)
test_data <- cbind(Lender_vector, Lender_percent_vector)
test_data <- as_tibble(test_data)
test_data$Lender_percent_vector <- as.numeric(test_data$Lender_percent_vector)
```
# The RColorBrewer Library - Dark2 Palette
```{r, warning=FALSE,echo=FALSE, message=FALSE}
# set the palette
library(RColorBrewer)
display.brewer.pal(8, "Dark2")
brewer.pal(8, "Dark2")
```
# The colors that I want: "#E6AB02","#666666"
# The colors that I get: "#D95F02", "#1B9E77"
```{r, warning=FALSE,echo=FALSE, message=FALSE}
# create the ggplot bar graph
library(ggplot2)
library(RColorBrewer)
plot.UCC <- ggplot(data=test_data,
aes(x = reorder(Lender_vector, -Lender_percent_vector), y = Lender_percent_vector,
fill=factor(ifelse(Lender_vector == "KUBOTA CREDIT CORP USA", "#E6AB02","#666666" )))) +
# colors that do appear = "#D95F02", "#1B9E77"
geom_bar(stat = "identity") +
scale_fill_brewer(palette = "Dark2") +
ggtitle("LENDER MARKET SHARE, %", subtitle = "All Equipment Types, All Lien Filing Types") +
labs(x="Lender", y="Percent") +
theme(plot.title =
element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
axis.text.x = element_text(size=7, angle = 60, hjust=1, vjust = 1),
plot.margin = margin(l=.5,t=.5,b=1,r=.5, "cm"),
legend.position = "none") +
ylim(0, 20)
plot.UCC
```