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
38 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
• 28,360 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
22 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
18 views
0 votes
0 answers
0 votes
1 answer

In a dpylr pipline how to use sample and seq?

For avoiding rowwise(), I prefer to use ...READ MORE

answered Apr 6, 2018 in Data Analytics by DeepCoder786
• 1,720 points
79 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
1,597 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,968 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
• 28,360 points
5,985 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
178 views
0 votes
1 answer

How can I add line to show mean for each barplot, not for all dataset?

Hey ranjith,  Use geom_hline() function to add reference lines ...READ MORE

answered Oct 13 in Data Analytics by Cherukuri
• 28,360 points
12 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
• 28,360 points
62 views