r - 更改堆积条形图上的字体颜色
问题描述
如何更改标签上的字体颜色?例如,我希望 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))
解决方案
我认为这行得通。
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"))
推荐阅读
- ruby-on-rails - 如何在 Rails 模型中调用可重用代码?
- time - 两个 Grafana 面板 singlestat 和具有相同查询的图形之间的区别
- c# - Xamarin Android Fit 标签
- apache - 如何从 httpd 2.2 迁移到 httpd 2.4 中的 conf.modules.d 目录?
- python - 从选项卡 tkinter 笔记本调用外部程序
- c++ - 矩阵乘法中的异步
- git - 我怎样才能将我的应用程序推送到heroku?
- kapacitor - Kapacitor:如何在 TICK 脚本中获取字段名称
- linq - Linq ForEach、Async Await 的区别
- makefile - Make没有注意到变化