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":
You can use the following syntax to change the legend labels in ggplot2: p + scale_fill_discrete(labels=c('label1', 'label2', 'label3', ...))
guides() can be used to alter legend title. To change the title with this function pass the required name to as an argument to guide_legend() function and this ultimately as the value to the attribute col. Example: R.
You can use the following syntax to change the order of the items in a ggplot2 legend: scale_fill_discrete(breaks=c('item4', 'item2', 'item1', 'item3', ...)
To change the color legend element border using ggplot2, we can use theme function where can put color in legend. key argument to desired color with element_rect.
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")
You specify the labels using labs()
, with each scale separately specified, i.e. labs(shape="Species label", colour="Petal width label")
.
If I understood your point correctly, you can simply use + labs(shape = "shape legend title", colour = "colour legend title")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With