在ggplot中,是否可以将图例中的线条更改为某种颜色

时间:2014-02-01 01:26:04

标签: r ggplot2 legend

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

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, 1.13, 1.14, 1.18, 1.22, 1.22, 1.23, 1.24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L, 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()

enter image description here

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

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

2 个答案:

答案 0 :(得分:0)

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

enter image description here

#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)

答案 1 :(得分:0)

只需使用:

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

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

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