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

使用scale_fill_discrete()后如何修改ggplot2图例标签字号?

修改ggplot图例标签字号的两种方法

你在使用scale_fill_discrete()自定义图例标签后,可通过以下两种方式调整标签字号:

方式一:通过theme()全局设置

直接在theme()中配置legend.text参数,该设置会应用到所有图例的标签:

theme(
  # 其他theme配置项...
  legend.text = element_text(size = 20) # 按需调整字号数值
)

方式二:通过guides()单独控制fill图例

如果只想针对fill图例的标签调整字号,可在guide_legend中通过label.theme参数单独设置:

guides(fill=guide_legend(title=NULL, label.theme = element_text(size = 20)))

修改后的完整代码

exp <- structure(list(year = c(1985L, 1995L, 2005L, 2020L, 2022L, 1985L, 1995L, 2005L, 2020L, 2022L, 1985L, 1995L, 2005L, 2020L, 2022L, 1985L, 1995L, 2005L, 2020L, 2022L, 1985L, 1995L, 2005L, 2020L, 2022L, 1985L, 1995L, 2005L, 2020L, 2022L, 1985L, 1995L, 2005L, 2020L, 2022L, 1985L, 1995L, 2005L, 2020L, 2022L), item = c("Social.security", "Social.security", "Social.security", "Social.security", "Social.security", "Culture..education.and.science.promotion.", "Culture..education.and.science.promotion.", "Culture..education.and.science.promotion.", "Culture..education.and.science.promotion.", "Culture..education.and.science.promotion.", "Bond.expenditure", "Bond.expenditure", "Bond.expenditure", "Bond.expenditure", "Bond.expenditure", "Grants.of.local.allocation.tax", "Grants.of.local.allocation.tax", "Grants.of.local.allocation.tax", "Grants.of.local.allocation.tax", "Grants.of.local.allocation.tax", "Public.works", "Public.works", "Public.works", "Public.works", "Public.works", "Measures.to.secure.food", "Measures.to.secure.food", "Measures.to.secure.food", "Measures.to.secure.food", "Measures.to.secure.food", "Sundry.expenses", "Sundry.expenses", "Sundry.expenses", "Sundry.expenses", "Sundry.expenses", "Other", "Other", "Other", "Other", "Other"), datos = c(9831L, 14548L, 20823L, 42998L, 36274L, 4904L, 6802L, 5779L, 9194L, 5390L, 10181L, 12857L, 19620L, 22326L, 24339L, 9690L, 12302L, 15923L, 16031L, 15656L, 6922L, 14216L, 8015L, 2241L, 1397L, 695L, 272L, 671L, 1498L, 1270L, 4347L, 6149L, 5382L, 23190L, 5835L, 6653L, 10888L, 10492L, 30119L, 17435L)), row.names = c(NA, -40L), class = "data.frame")

expenditure <- ggplot(data=exp, aes(x=as.factor(year), y=datos, fill=item)) +
  scale_fill_discrete(labels = c("Bond expenditure", "Culture, education and science", "Grants of local allocation tax","Measures to secure food","Other","Public works","Social security","Sundry expenses")) + 
  geom_bar(stat="identity") +
  ylab("billions of yen") + 
  xlab("") + 
  guides(fill=guide_legend(title=NULL)) +
  theme(
    axis.title.x = element_text(color="black", size=18, face="bold"),
    axis.title.y = element_text(color="black", size=18, face="bold"),
    legend.text = element_text(size = 20), # 调整图例标签字号
    plot.margin = margin(1, 2, 1, 1, "cm"),
    panel.background = element_rect(fill='transparent'),
    plot.background = element_rect(fill='transparent', color=NA),
    rect = element_rect(fill = "transparent"),
    axis.text.x=element_text(angle=90,hjust=1,vjust=0.5),
    axis.text = element_text(size = 18, colour = "black", face = "bold"),
    legend.position = c(0.2, 0.8)
  )

ggsave('expenditure.png', expenditure, bg='transparent', width = 13, height = 13)

注:原代码中scale_color_discrete(name="")属于冗余配置(图表未使用color美学),已删除。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 13:44:56