更改堆叠条形图上的字体颜色

时间:2019-09-20 17:04:31

标签: r ggplot2 label bar-chart

如何更改标签上的字体颜色?例如,我希望A的标签为白色,B和C的标签为黑色。另外,我想将标签设为粗体。


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

1 个答案:

答案 0 :(得分:0)

我认为这可行。

head -3 file2.txt
 Side A,Age(us),mm:ss.ms_us_ns_ps
 84 Vendor Specific, 0000000000418, 0349575322
 83 Vendor Specific, 0000000002099, 0349575343
 83 Vendor Specific, 0000000001628, 0349576662

enter image description here