Adding Simple Legend to Plot in R

Adding simple legend to plot in R

Take a look at ?legend and try this:

legend('topright', names(a)[-1] , 
lty=1, col=c('red', 'blue', 'green',' brown'), bty='n', cex=.75)

Sample Image

Add legend to a plot with R

legend( "topleft", c("cars", "trucks", "cars1", "trucks1"), 
text.col=c("blue", "red", "yellow", "green") )

Use the ?legend command to see options, such other locations on the graph, besides "topleft" or text size, whether to have a box around the legend, etc.

Adding a legend to gf_line plot in R

First of all, I would highly recommend you to use ggplot2 which is way easier to use. What you could do is create a column with the value you want to display in your legend and add this column name to your color argument which will be the legend title like this:

library(ggformula)
returns1.bw[,1]<-as.Date(returns1.bw[,1])
returns2.bw[,1]<-as.Date(returns2.bw[,1])
# Add column with the name for in legend
returns1.bw$legend <- "Equation 1"
returns2.bw$legend <- "Equation 2"

gf_line(mean ~ date, data= returns1.bw,color=~legend) %>%
gf_line(mean ~ date,data= returns2.bw, color=~legend) %>%
gf_hline(yintercept= 0,linetype= 2)
#> Warning: geom_hline(): Ignoring `mapping` because `yintercept` was provided.

Sample Image

Created on 2022-08-20 with reprex v2.0.2

R: adding a plot legend in R

As @rawr says, palette() determines the colour sequence used. If you use integers to specify colours, it will also look at palette(). Thus

with(iris,plot(Sepal.Length, Sepal.Width, col = Species))
legend("topright",legend=levels(iris$Species),col=1:3, pch=1)

works nicely.

Sample Image

Base R doesn't have an auto-legend facility: the ggplot2 package does.

library(ggplot2)
ggplot(iris,aes(Sepal.Length,Sepal.Width,colour=Species))+geom_point()

gives you a plot with an automatic legend (use theme_set(theme_bw()) if you don't like the grey background).

Sample Image

The built-in lattice package can also do automatic legends:

library(lattice)
xyplot(Sepal.Width~Sepal.Length,group=Species,data=iris,auto.key=TRUE)

Sample Image

Adding a legend to a plot

One way that you can do this is to use par to increase the margin at the top and also to enable writing outside the plot region. Then you can use legend with a negative inset.

## Your graph
par(mar=c(5.1,4.1,6,2.1), xpd=TRUE)
plot(perf, col="red")
plot(perf2, add = TRUE, col="blue")

## Add Legend
legend("topright", c("Pred1", "Pred2"), lty=1,
col = c("red", "blue"), bty="n", inset=c(0,-0.15))

Legend in margin

ggplot Adding manual legend to plot without modifying the dataset

If you want to have a legend then you have to map on aesthetics. Otherwise scale_color/fill_manual will have no effect:

vec <- c(rep(1, 100), rep(2, 100), rep(3, 80), rep(4, 70), rep(5, 60))
tbl <- data.frame(value = vec)

mean_vec <- mean(vec)

cols <- c(
"Frequency" = "grey",
"mean" = "blue"
)

library(ggplot2)

ggplot(tbl) +
aes(x = value) +
geom_histogram(aes(fill = "Frequency"), binwidth = 1) +
geom_vline(aes(color = "mean", xintercept = mean_vec), size = 1) +
theme_minimal() +
scale_color_manual(values = "blue") +
scale_fill_manual(name = "Test", values = "grey")

Sample Image



Related Topics



Leave a reply



Submit