使用两个图例更改ggplot中的两个图例标题

时间:2011-08-02 11:00:38

标签: r ggplot2 legend

我的ggplot上有两个传说,有两个不同的图例标题(从ggplot()自动创建)。现在,我想改变这个传奇的标题。 + labs(colour = "legend name")仅更改第二个图例标题。我怎么能改变第一个呢?

示例数据:

dataset <- structure(list(date = structure(c(1264572000, 1266202800, 1277362800), 
class = c("POSIXt", "POSIXct"), tzone = ""), 
x1 = c(-0.00183760994446658, 0.00089738603087497, 0.000423513598318936), 
x2 = c("approach x","approach y","approach z"), 
x3 = c("Type1", "Type1", "Type2")) ,
.Names = c("date", "data","code","type"),
row.names = c("1", "2", "3"), class = "data.frame")

这是我制作剧情的代码:

p <- ggplot(dataset, aes(x=date, y=data)) +
geom_point(aes(shape = factor(type), color = code)) +
scale_shape_manual(value=c(23,15))
print(p)

图例标题默认为:“factor(type)”和“code”: enter image description here

2 个答案:

答案 0 :(得分:57)

以下是使用iris数据集的示例:

data(iris)
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width)) +
    geom_point(aes(shape=Species, colour=Petal.Width)) + 
    scale_colour_gradient() +
    labs(shape="Species label", colour="Petal width label")

您使用labs()指定标签,并分别指定每个比例,即labs(shape="Species label", colour="Petal width label")

enter image description here

答案 1 :(得分:11)

如果我理解你的观点,你可以简单地使用+ labs(shape = "shape legend title", colour = "colour legend title")