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,690 points
3,535 views

10 answers to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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,550 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
• 10,290 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

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 6, 2018 in Data Analytics by Maverick
• 10,000 points
32 views
0 votes
1 answer

Splunk Cluster instalation document was not uploaded in the cource.

Dear Learner, Greetings ! We would like to inform ...READ MORE

answered Dec 18, 2017 in Data Analytics by Sudhir
• 1,610 points
45 views
+1 vote
1 answer

Custom Function to replace missing values in a vector with the mean of values

You have missed out on "na.rm=TRUE" inside ...READ MORE

answered Mar 27, 2018 in Data Analytics by Bharani
• 4,550 points
31 views
0 votes
1 answer

What are the options for deploying models in production with R?

Well, I could say that the answer ...READ MORE

answered Apr 12, 2018 in Data Analytics by DataKing99
• 8,100 points
131 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,700 points
944 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,100 points
74 views
0 votes
1 answer

Aligning title in ggplot2

You can use the theme layer to ...READ MORE

answered Apr 27, 2018 in Data Analytics by Bharani
• 4,550 points
469 views
0 votes
1 answer

How to change fill color in each facet using ggplot2?

You can map the facetting variable to ...READ MORE

answered May 8, 2018 in Data Analytics by kappa3010
• 2,010 points
1,499 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,550 points
1,309 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,550 points
29 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.