用两个图例更改 ggplot 中的两个图例标题
我的 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)
图例标题默认为:“因子(类型)”和“代码”:
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?
Sample data:
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":
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下是使用
iris
数据集的示例:您可以使用
labs()
指定标签,并单独指定每个尺度,即labs(shape="Species label" , color="花瓣宽度标签")
。Here is an example using the
iris
dataset:You specify the labels using
labs()
, with each scale separately specified, i.e.labs(shape="Species label", colour="Petal width label")
.如果我正确理解你的观点,你可以简单地使用 + labs(shape = "shape legend title", color = "colour legend title")
If I understood your point correctly, you can simply use
+ labs(shape = "shape legend title", colour = "colour legend title")