用两个图例更改ggplot中的两个图例标题 [英] change both legend titles in a ggplot with two legends
问题描述
我在ggplot上有两个不同的图例标题(从 ggplot()
)自动创建。现在,我想改变这个传说标题。 + labs(color =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)
图例标题默认为:factor(type)和code:
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, color =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屋!