How to put border for all legends together and separate borders for each legend in ggplot?

0 votes
How to put border for all legends together and separate borders for each legend in ggplot?
Jul 23 in Data Analytics by Anitha
25 views

1 answer to this question.

0 votes

Hi Anitha,

There are 2 functions used to edit legend and all legends together.

Legend.background / text / title /  direction / position, etc provide design and formating to each legends for all geom_* layers in the plot whereas 

legend.box.background / text / title /  direction / position provides format, edit the legends for all legends together.

An example - 

ggplot(mpg, aes(displ, hwy, col = class)) + geom_point(size = 2,aes(shape = factor(cyl))) + theme(legend.box.background = element_rect(color = "red",fill = "grey"),legend.box.margin = margin(0.4,0.4,0.4,0.4,"cm"),legend.background = element_rect(color = "yellow")) + guides(shape = guide_legend(title = "cyl")) + scale_shape_discrete(labels = c("a","b","c","d")) + theme(legend.position = "bottom", legend.direction = "vertical")

The above code gives the below plot.

Here legend.key.background provides background color to the container that holds all legends whereas legend.background give color for each legend container.

answered Jul 24 by Cherukuri
• 25,900 points

Related Questions In Data Analytics

0 votes
0 answers

how to hide and show legends for different geom_plots* in a single ggplot?

how to hide and show legends for ...READ MORE

Jul 23 in Data Analytics by karthick
17 views
0 votes
0 answers

How to add color legend for box plots in R?

How to add color legend for box ...READ MORE

Jul 3 in Data Analytics by keerthi
14 views
0 votes
0 answers
0 votes
0 answers
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
1,435 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,020 points
2,574 views
0 votes
11 answers

Changing the legend title in ggplot

Hi, you can also try guides() to ...READ MORE

answered Jul 30 in Data Analytics by Cherukuri
• 25,900 points
5,304 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,130 points
156 views
0 votes
1 answer

How to replace all occurrences of a character in a character column in a data frame in R

If you used sub() to replace the ...READ MORE

answered Jun 28 in Data Analytics by anonymous
• 25,900 points
48 views
0 votes
1 answer

How to add labels on bars in bar chart in ggplot?

Use geom_text() to add labels on the ...READ MORE

answered Aug 20 in Data Analytics by anonymous
• 25,900 points
19 views