I have made this scatter plot with the help of ggplot2:

`ggplot(data = mtcars,aes(x=mpg,y=disp,col=factor(cyl)))+geom_point()`

I would want to change the title of legend from 'factor(cyl)' to cylinder

May 24, 2018 14,917 views

## 11 answers to this question.

`ggplot(data = mtcars,aes(x=mpg,y=disp,col=factor(cyl)))+geom_point()+labs(col="cylinder")`

• 4,660 points
Dude, you are a genius!!!

Nobody have explained this, and this is the only one on the internet that works!.

Explanation --- look at your plot to see what the legend is referring then name it accordingly. In the above graph, the legend is color by factor of cyl. Bharani changed the labs(color = "cylinder").

Thanks man!
Hey @Ken,Thanks for the explanation, makes the solution more clear. It would be great if you post this as an answer so that it's more readable and understandable for others.

Try something like this:

```library(reshape2)
dfm <- melt(df, id = "TY")
# creating a scatterplot
ggplot(data = dfm, aes(x = TY, y = value, color = variable)) +
geom_point(size=5) +
labs(title = "Temp\n", x = "TY [°C]", y = "Txxx", color = "Title\n") +
scale_color_manual(labels = c("T999", "T888"), values = c("blue", "red")) +
theme_bw() +
theme(axis.text.x = element_text(size = 14), axis.title.x = element_text(size = 16),
axis.text.y = element_text(size = 14), axis.title.x = element_text(size = 16),
plot.title = element_text(size = 20, face = "bold", color = "red"))```
• 11,340 points

If you only want to change the legend text labels and not the colours from ggplot's default palette, you can use scale_color_hue(labels = c("T999", "T888")) instead of scale_color_manual().

answered Dec 10, 2018 by Kailash

Try this:

```ggplot(mtcars, aes(x=mpg, y=disp, size=hp, col=as.factor(cyl), shape=as.factor(gear))) +
geom_point() +
labs(x="miles per gallon", y="displacement", size="horsepower",
col="# of cylinders", shape="# of gears")```
answered Dec 10, 2018 by Haid
`ggplot(data = mtcars,aes(x=mpg,y=disp,col=factor(cyl)))+geom_point()+labs(col="cylinder")`

look at your plot to see what the legend is referring then name it accordingly. In the above graph, the legend is color by factor of cyl. Change the labs(color = "cylinder").

answered Dec 10, 2018 by Anurag

To change legend position:

```p + theme(legend.position="top")
p + theme(legend.position="bottom")```
answered Dec 10, 2018 by Aisha

Try this:

```# legend title
p + theme(legend.title = element_text(colour="blue", size=10, face="bold"))
# legend labels
p + theme(legend.text = element_text(colour="blue", size=10, face="bold"))```
answered Dec 10, 2018 by Vikas

Chnage background color of the legend box:

```# legend box background color p + theme(legend.background = element_rect(fill="lightblue", size=0.5, linetype="solid"))

p + theme(legend.background = element_rect(fill="lightblue", size=0.5, linetype="solid", colour ="darkblue"))```
answered Dec 10, 2018 by Vedant

You can use the function labs(). Its used to modify axis, legend and plot labels.

Good labels are critical for making your plots accessible to a wider audience. Ensure the axis and legend labels display the full variable name. Use the plot title and subtitle to explain the main findings. It's common to use the caption to provide information about the data source.

Have a look at the r documentations for detailed explanation.

answered Dec 10, 2018 by Keshav

Example :

```p <- ggplot(mtcars, aes(mpg, wt, colour = cyl)) + geom_point()
p + labs(colour = "Cylinders")
p + labs(x = "New x label")

# The plot title appears at the top-left, with the subtitle
# display in smaller text underneath it
p + labs(title = "New plot title")
p + labs(title = "New plot title", subtitle = "A subtitle")

# The caption appears in the bottom-right, and is often used for
p + labs(caption = "(based on data from ...)")```
answered Dec 10, 2018 by Rajni

Hi, you can also try guides() to change the legend title

Try below code -

`ggplot(data = mtcars,aes(x=mpg,y=disp,col=factor(cyl)))+geom_point() + guides(color = guide_legend(title = "Cylinder"))`

Hope it helps!

• 33,010 points

## How do I modify the background grid in ggplot?

You can use the function called background_grid().  This ...READ MORE

## Arrange the order of axis elements in ggplot

How to arrange the order of axis ...READ MORE

## Change the order of legend values in a plot in R

@prardhana, Use scale_fill/color/size_discrete/contin....(labels = c()). compare both to see ...READ MORE

+1 vote

## How to plot side-by-side Plots with ggplot2 in R?

By Using gridExtra library we can easily ...READ MORE

## How to order bars in a bar graph using ggplot2?

The key to ordering is to set ...READ MORE

## Split plot into multiple plots corresponding to each value of the field in R

Use facet_grid() and pass the field to ...READ MORE

## Chart/plot to show 3 dimensions in a chart - R

Hi, I want to show chart with ...READ MORE