You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 11:26:02