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

如何让ggplot图例同时展示各分类的颜色与对应线型?

问题描述

我用Eurostat的数据集绘制折线图,将「Zona Euro」的线条设置为虚线,但图例中该分类仅显示实线,如何让图例也显示对应的虚线?

数据集

structure(list(geo = c("Espanya", "Unió Europea", "França", 
"Zona Euro", "Espanya", "Unió Europea", "França", "Zona Euro", 
"Espanya", "Unió Europea", "França", "Itàlia", "Zona Euro", 
"Espanya", "Unió Europea", "França", "Itàlia", "Zona Euro", 
"Espanya", "Unió Europea", "França"), time = structure(c(19297, 
19297, 19297, 19266, 19266, 19266, 19266, 19236, 19236, 19236, 
19236, 19236, 19205, 19205, 19205, 19205, 19205, 19174, 19174, 
19174, 19174), class = "Date"), values = c(11.8, 5.3, 5.9, 5.8, 
11.7, 5.3, 5.9, 5.8, 11.5, 5.2, 5.8, 7, 5.8, 11.3, 5.3, 5.9, 
7, 5.8, 11.3, 5.3, 6.1)), row.names = c(NA, -21L), class = c("tbl_df", 
"tbl", "data.frame"))

绘图代码

ggplot(taxa_atur, aes(x=time, y=values, color= geo, group=geo, linetype=geo))+
  geom_line(linewidth=0.7, aes(linetype = geo)) +
    scale_colour_brewer(name="", palette = "Set1") +
  scale_linetype_manual(values = geo_linetype) +
  scale_y_continuous(expand=c(0,0), limits=as.numeric(c(5,15)), breaks=seq(from=5, to=15, by=1)) + 
  scale_x_date(expand=c(0,0), breaks = "2 months", labels=date_format("%b %y"),
               limits = c(as.Date("2020-12-01"), NA)) +
  geom_hline(aes(yintercept = 0), color="black")+
  labs(title="Taxa d'atur",
       subtitle= "(% de la població entre 25 i 74 anys)",
       y="",
       x=" ",
       cation="Font: Eurostat. Taxa d'atur corregida d'estacionalitat.")+
  theme_minimal()+
  theme(
    text=element_text(family="serif", color="black"),
    panel.grid.major.y = element_line(color="grey90"),
    panel.grid.major.x = element_line(color="white"),
    panel.grid.minor = element_blank(),
    axis.line = element_line(colour = "black"),
    plot.title = element_text(size=12, face="bold"),
    plot.subtitle = element_text(size=10),
    plot.caption = element_text(hjust = 0),
    plot.title.position = "plot",
    plot.caption.position = "plot",
    legend.position = "bottom",
    legend.justification = "left",
    legend.direction = "horizontal",
    legend.text = element_text(size = 12),
    legend.box.margin = margin(0,0,0,0,"cm"),
    strip.text.x = element_text(size=15),
    axis.line.x=element_line(color="black"),
    axis.line.y=element_line(color="black"),
    axis.text.x = element_text(size=10),
    axis.text.y = element_text(size=10),
    axis.title.x = element_text(size=10, face="bold",
                                margin = margin(t=12, b= 0, r = 11, l = 5)),
    axis.title.y = element_text(size=10, face="bold",
                                margin = margin(t=0, b= 0, r = 11, l = 5)))
解决方案

问题可通过两个关键调整解决:

  • 确保geo_linetype覆盖所有地理类别:手动定义线型时,要给每个geo分类指定对应样式,尤其要把「Zona Euro」设为虚线("dashed"),避免因类别遗漏或顺序错误导致图例样式不匹配。示例定义:
geo_linetype <- c(
  "Espanya" = "solid",
  "Unió Europea" = "solid",
  "França" = "solid",
  "Zona Euro" = "dashed",
  "Itàlia" = "solid"
)
  • 合并颜色与线型图例:由于同时用color和linetype映射geo,需要让ggplot合并两个图例,保证图例线条样式与图表一致。在代码中添加guides设置:
guides(
  color = guide_legend(override.aes = list(linetype = geo_linetype)),
  linetype = "none" # 隐藏单独的线型图例
)

另外,注意原代码中labs里的笔误:cation应改为caption,修正后才能正确显示图表来源说明。

调整后,「Zona Euro」的图例线条会和图表中的虚线样式保持一致。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 12:25:00