To change the Size of Legend, we have to add guides() and guide_legend() functions to the geom_point() function. Inside guides() function, we take parameter color, which calls guide_legend() guide function as value. Inside guide_legend() function, we take an argument called override.
To change the legend size of the plot, the user needs to use the cex argument of the legend function and specify its value with the user requirement, the values of cex greater than 1 will increase the legend size in the plot and the value of cex less than 1 will decrease the size of the legend in the plot.
To change the default size of legend text, we use rc() method and pass a keyword argument fontsize. To add a title to the plot, we use title() function.
You can use the following syntax to change the legend labels in ggplot2: p + scale_fill_discrete(labels=c('label1', 'label2', 'label3', ...))
You can use theme_get()
to display the possible options for theme.
You can control the legend font size using:
+ theme(legend.text=element_text(size=X))
replacing X with the desired size.
theme(plot.title = element_text(size = 12, face = "bold"),
legend.title=element_text(size=10),
legend.text=element_text(size=9))
You can also specify the font size relative to the base_size
included in themes such as theme_bw()
(where base_size
is 11) using the rel()
function.
For example:
ggplot(mtcars, aes(disp, mpg, col=as.factor(cyl))) +
geom_point() +
theme_bw() +
theme(legend.text=element_text(size=rel(0.5)))
A simpler but equally effective option would be:
+ theme_bw(base_size=X)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With