Changing the legend title in ggplot

0 votes

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

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

image

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

May 24, 2018 in Data Analytics by zombie
• 3,790 points
17,644 views

11 answers to this question.

0 votes

You just need to add one more command to your code:

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

answered May 24, 2018 by Bharani
• 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.
0 votes

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"))
answered Dec 10, 2018 by Ali
• 11,360 points
0 votes

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
0 votes

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
0 votes
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
0 votes

To change legend position:

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

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
0 votes

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
0 votes

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
0 votes

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
# sources, notes or copyright
p + labs(caption = "(based on data from ...)")
answered Dec 10, 2018 by Rajni
0 votes

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!

answered Jul 30, 2019 by Cherukuri
• 33,030 points

Related Questions In Data Analytics

0 votes
1 answer

How do I modify the background grid in ggplot?

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

answered Dec 7, 2018 in Data Analytics by Maverick
• 10,840 points
973 views
0 votes
0 answers

Arrange the order of axis elements in ggplot

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

Jul 17, 2019 in Data Analytics by likitha
733 views
0 votes
1 answer

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

answered Nov 4, 2019 in Data Analytics by payal
1,981 views
+1 vote
2 answers

change the color for bars in geom_bar in ggplot

Hi @radha, You can have 2 types of ...READ MORE

answered Jul 24, 2019 in Data Analytics by Cherukuri
• 33,030 points
35,745 views
0 votes
1 answer

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

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

answered Apr 16, 2018 in Data Analytics by DeepCoder786
• 1,720 points

edited Jun 9, 2020 by MD 8,838 views
0 votes
1 answer

How to order bars in a bar graph using ggplot2?

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

answered Jun 1, 2018 in Data Analytics by DataKing99
• 8,250 points
1,169 views
0 votes
1 answer
0 votes
0 answers

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

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

Jul 12, 2019 in Data Analytics by kalyan
543 views
0 votes
1 answer

Changing the order of bars in a bar-plot - ggplot2 - R

You can use the scale_x_discrete() function with ...READ MORE

answered May 28, 2018 in Data Analytics by Bharani
• 4,660 points
9,722 views
0 votes
1 answer

Plotting multiple graphs on the same page in R

If you want to plot 4 graphs ...READ MORE

answered Mar 27, 2018 in Data Analytics by Bharani
• 4,660 points
1,426 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP