1

How do I change the font colors on the labels? For example, I'd like the labels for A to be white and the labels for B and C to be black. Also, I'd like to make the labels bold.


Vintage <- c(201801,201801,201801,201802,201802,201802,201803,201803,201803)
Grade <- c("A","B","C","A","B","C","A","B","C")
OrigAmt <- c(3.5,0.884,0.606,6.31,2.31,1.12,6.80,1.90,1.05)
freq <- c(70.2,17.7,12.1,64.7,23.7,11.5,69.8,19.5,10.8)
dat <-as.tibble(as.data.frame(cbind(Vintage,Grade,OrigAmt,freq)))
dat$OrigAmt <- as.double(as.character(dat$OrigAmt))
dat$freq <- as.double(as.character(dat$freq))

library(ggplot2)

ggplot(data = dat, 
       aes(y = freq, x = Vintage, fill = fct_rev(Grade))) +
  geom_col() +
  geom_text(aes(label = paste0(freq,"%")),
            position = position_stack(vjust = 0.5), size = 3) +
  scale_y_continuous(labels = dollar_format(suffix = "%", prefix = "")) +
  labs(title = "Distribution of Originations by Vintage",
       subtitle = "Source: ") +
  labs(x = NULL, y = "Percentage") +
  theme_bw() +
  theme(legend.position = "bottom", 
        legend.direction = "horizontal",
        legend.title = element_blank()) +
  guides(fill = guide_legend(reverse = T)) +
  scale_fill_manual(values = c("grey", "gray40", "brown")) +
  theme(axis.text.x = element_text(angle = 90),
        axis.text.x.bottom = element_text(vjust = 0.5))

enter image description here

M--
  • 25,431
  • 8
  • 61
  • 93
BuJay
  • 115
  • 1
  • 12
  • Add a color scale to the text the way you've added a fill scale already. See the docs for `geom_text` for the options available, one of which is `fontface` for things like bold. One of the examples shows this. – camille Sep 20 '19 at 17:07
  • 2
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Pictures of data are not helpful because we can't copy/paste them into R. – MrFlick Sep 20 '19 at 17:08
  • thanks for that feedback - I have adjusted the original question to be reproducible. – BuJay Sep 20 '19 at 17:19
  • if I add the following: geom_col() + geom_text(aes(label = paste0(freq,"%"), colour = Grade), position = position_stack(vjust = 0.5), size = 3) + then the label colors change but their positions are no longer centered in the boxes – BuJay Sep 20 '19 at 17:32
  • 2
    To match the stacking order based on fill, don't you need `fct_rev(Grade)` for color? – aosmith Sep 20 '19 at 17:37
  • thanks @aosmith! That did move me closer....then I was getting a second legend etc. I think I have a solution - will post. – BuJay Sep 20 '19 at 17:56

1 Answers1

0

I think this works.

library(ggplot2)
Vintage <- c(201801,201801,201801,201802,201802,201802,201803,201803,201803)
Grade <- c("A","B","C","A","B","C","A","B","C")
OrigAmt <- c(3.5,0.884,0.606,6.31,2.31,1.12,6.80,1.90,1.05)
freq <- c(70.2,17.7,12.1,64.7,23.7,11.5,69.8,19.5,10.8)
dat <-as.tibble(as.data.frame(cbind(Vintage,Grade,OrigAmt,freq)))
dat$OrigAmt <- as.double(as.character(dat$OrigAmt))
dat$freq <- as.double(as.character(dat$freq))

ggplot(data = dat, 
       aes(y = freq, x = Vintage, fill = fct_rev(Grade))) +
  geom_col() +
  geom_text(aes(label = paste0(freq,"%"),
                colour = fct_rev(Grade)),
            position = position_stack(vjust = 0.5), 
            size = 3,
            show.legend = FALSE,
            fontface="bold") +
  scale_y_continuous(labels = dollar_format(suffix = "%", prefix = "")) +
  labs(title = "Distribution of Originations by Vintage",
       subtitle = "Source: ") +
  labs(x = NULL, y = "Percentage") +
  theme_bw() +
  theme(legend.position = "bottom", 
        legend.direction = "horizontal",
        legend.title = element_blank()) +
  guides(fill = guide_legend(reverse = T)) +
  scale_fill_manual(values = c("grey", "gray40", "black")) +
  theme(axis.text.x = element_text(angle = 90),
        axis.text.x.bottom = element_text(vjust = 0.5)) +
  scale_color_manual(values = c("black", "white", "white"))

enter image description here

BuJay
  • 115
  • 1
  • 12