【问题标题】:In ggplot, Is it possible to change the lines in the legend to some arbitrary color在ggplot中,是否可以将图例中的线条更改为任意颜色
【发布时间】:2026-01-28 23:25:03
【问题描述】:

假设我有一个如下所示的任意数据集:

foo <- structure(list(y = c(-2.51, -2.12, -2.03, -1.9, -1.83, -1.78, 
-1.75, -1.73, -1.67, -1.62, -1.61, -1.5, -1.47, -1.45, -1.44, 
-1.39, -1.28, -1.27, -1.19, -1.17, -1.14, -1.14, -1.13, -1.12, 
-1.07, -1.07, -1.07, -1.06, -1.06, -1.04, -1.02, -1.01, -1, -0.99, 
-0.98, -0.97, -0.95, -0.95, -0.95, -0.94, -0.93, -0.9, -0.9, 
-0.87, -0.87, -0.86, -0.84, -0.84, -0.84, -0.83, -0.81, -0.8, 
-0.77, -0.76, -0.73, -0.71, -0.71, -0.69, -0.69, -0.66, -0.66, 
-0.65, -0.65, -0.64, -0.58, -0.57, -0.55, -0.54, -0.53, -0.51, 
-0.5, -0.47, -0.47, -0.46, -0.45, -0.4, -0.4, -0.39, -0.39, -0.38, 
-0.35, -0.31, -0.3, -0.3, -0.3, -0.29, -0.27, -0.24, -0.23, -0.23, 
-0.22, -0.19, -0.18, -0.17, -0.15, -0.14, -0.14, -0.13, -0.12, 
-0.12, -0.11, -0.1, -0.08, -0.04, -0.03, -0.03, -0.03, -0.02, 
0, 0.01, 0.01, 0.05, 0.1, 0.11, 0.12, 0.12, 0.13, 0.16, 0.16, 
0.16, 0.17, 0.17, 0.18, 0.19, 0.2, 0.21, 0.22, 0.23, 0.24, 0.25, 
0.26, 0.26, 0.26, 0.26, 0.26, 0.28, 0.29, 0.29, 0.3, 0.31, 0.31, 
0.33, 0.34, 0.34, 0.36, 0.45, 0.51, 0.51, 0.52, 0.53, 0.54, 0.55, 
0.56, 0.56, 0.63, 0.64, 0.67, 0.67, 0.69, 0.7, 0.72, 0.72, 0.74, 
0.75, 0.77, 0.79, 0.86, 0.91, 0.95, 0.98, 1, 1.02, 1.05, 1.07, 
1.11, 1.13,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1L, 1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L)), .Names = c("y", "x", "condition"), row.names = c(NA, 200L
), class = "data.frame")

我可以制作如下图:

ggplot(foo, aes(x, y, color=factor(condition))) + geom_line()

假设无论出于何种原因,我都不需要图例中具有不同颜色的四行。相反,我只想在图例中有一条黑线。然而,实际绘图的规格(例如颜色、线型等)似乎映射到图例上。所以我不确定如何更改它。

所以我的问题是如何任意设置图例:任意颜色、线型和文字

【问题讨论】:

  • 试试+ guides(colour=guide_legend(override.aes=list(colour="black")))。也看看cloud.github.com/downloads/hadley/ggplot2/guide-col.pdf
  • 更一般地说,如果你想手动构建任意图例,ggplot 真的不是一个很好的工具。它的设计主要是为了相反的意图。如果您想对您的图例(以及随之而来的繁琐工作)进行如此多的控制,您可能会更乐意使用基本图形。
  • @joran True...这是我今天意识到的...在花了几个小时摆弄ggplot的各个方面...
  • @Alex,如果你有机会看看 Winston Chang 的出色 R Graphics Cookbook。从第 225 页开始,您将在 ggplot 中找到近 20 页的操纵图例,解决您关于任意颜色、线型和文本的所有问题。我看不出这比使用基本图形更复杂,相反,我发现它更容易.

标签: r ggplot2 legend


【解决方案1】:

你可以作弊 - 用ggplot() 伪造传说并使用grid.arrange() 将它们组合在一起:

#pre-reqs
require(ggplot2)
require(gridExtra)

#make a blank background theme
blank_theme<-theme(axis.line=element_blank(),
                   axis.text.x=element_blank(),
                   axis.text.y=element_blank(),
                   axis.ticks=element_blank(),
                   axis.title.x=element_blank(),
                   axis.title.y=element_blank(),
                   legend.position="none",
                   panel.background=element_blank(),
                   panel.border=element_blank(),
                   panel.grid.major=element_blank(),
                   panel.grid.minor=element_blank(),
                   plot.background=element_blank())

#main plot, without legend
g.plot<-ggplot(foo, aes(x, y, color=factor(condition))) + geom_line() + theme(legend.position="none")

#legend based on tile, arc and title
g.legend<-qplot(0.5,0.5,geom="blank") + blank_theme +
  geom_tile(aes(1,1),fill="lightgrey", color="black") + 
  geom_text(aes(x=1,y=2,label="TITLE"),size=3) +
  geom_segment(aes(x=0.7,xend=1.3,y=1,yend=1)) +
  coord_fixed(1,c(0.4,1.6),c(0.4,2.3))

#use grid arrange to layout
grid.arrange(g.plot, g.legend, widths=c(0.9, 0.1), ncol=2, nrow=1)

【讨论】:

    【解决方案2】:

    只需使用:

    ggplot(foo, aes(x, y, group=factor(condition), color="red")) + geom_line()
    

    group=factor(condition) 确保绘制不同条件的线条,color="red" 设置所有线条的颜色。排除color="red" 部分将导致黑线。

    有关图例的更多样式提示,请参阅online R Cookbook

    【讨论】: