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

R中使用scale_fill_gradientn时如何调整ggplot2图例的字体大小与粗体

你需要在现有代码的theme()配置项中,新增图例相关的样式参数即可:

  • 调整图例标题的样式用legend.title参数
  • 调整图例刻度文本的样式用legend.text参数

修改后的完整代码如下:

yellowgreen <- c("yellow", "dark green") 
pal <- colorRampPalette(yellowgreen)(100)
library(ggplot2)

data <- read.table("file_directory", sep=";", header = T)

v <- ggplot(data, aes(x = reorder(A, B) , y = Pourcentage, fill = B))
v + geom_tile() + geom_text(aes(label = round(B,1)), color = "black", size = 4, fontface=2) + 
scale_fill_gradientn(colors = pal, name = expression("Legend Title")) + 
labs(y = "Title for Y", x ="Title for X") +
theme(axis.title.x = element_text(face="bold",size=24, color="black"), 
      axis.text.x = element_text(face="bold",size=24, color="black"), 
      axis.title.y = element_text(face="bold",size=24, color="black"), 
      axis.text.y = element_text(face="bold", size=24, color="black"),
      # 新增以下两行配置图例样式,可自行调整size参数修改字号
      legend.title = element_text(face="bold", size=24, color="black"),
      legend.text = element_text(face="bold", size=22, color="black"))

内容的提问来源于stack exchange,提问作者iforire

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 04:45:02