使用两个图例更改 ggplot 中的两个图例标题 [英] change both legend titles in a ggplot with two legends

查看:37
本文介绍了使用两个图例更改 ggplot 中的两个图例标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 ggplot 上有两个图例,有两个不同的图例标题(从 ggplot() 自动创建).现在,我想更改此图例标题.+ labs(colour = "legend name") 只更改第二个图例标题.我怎样才能改变第一个?

I have two legends on my ggplot with two different legend titles (automatically created from ggplot()). Now, I want to change this legend titles. + labs(colour = "legend name") only change the second legend title. How can I change the first one, too?

示例数据:

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

这是我生成情节的代码:

Here is my code to produce the plot:

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)

图例标题默认为:因子(类型)"和代码":

The legend titles are on default: "factor(type)" and "code":

推荐答案

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

Here is an example using the iris dataset:

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").

You specify the labels using labs(), with each scale separately specified, i.e. labs(shape="Species label", colour="Petal width label").

这篇关于使用两个图例更改 ggplot 中的两个图例标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆