如何将ggplot图表的标题文字颜色从黑色改为红色?
修改ggplot图表标题文字颜色的方法
当然可以修改标题颜色,你只需要在theme()中指定plot.title的颜色参数即可。直接在现有代码的theme_bw()后添加对应的设置,就能把标题改成红色:
修改后的完整代码:
q <- ggplot(data = graph_data, aes(x = Year_NA)) + geom_line(aes(y = MTCT_rate_at_before_birth_mean, color = "blue"))+ geom_line(aes(y = Postnatal_MTCT_rate_to_18_months_from_HIV_diagnosed_mothers_mean, color = "orange"))+ labs(title= "MTCT rate postnatal vs. prenatal Vs. Year", y="MTCT Rate", x= "Year") + scale_colour_discrete(name = "", labels = c("MTC before birth", "MTCT after birth")) + theme_bw() + theme(plot.title = element_text(color = "red")) # 设置标题为红色 show(q)
补充说明
element_text()里还可以调整标题的字号、字体、对齐方式等,比如element_text(color = "red", size = 14, hjust = 0.5)可以让标题居中且字号更大。- 如果需要同时修改副标题、坐标轴标题的颜色,可分别用
plot.subtitle、axis.title.x、axis.title.y对应设置。
内容的提问来源于stack exchange,提问作者Corrin Merry
相关产品推荐
相关产品推荐

