Ggplot teckenstorlek av legenden
In you can control the size of the symbols used in the legend by overriding the aesthetic of the legend using the parameter of the function.
As an example to adjust the point size of the points displayed in a color legend you can do:
But lets see how it works on an example.
The libraries
- : For this particular situation you can customize the appearance of the items in a plots legend by using the standard tools provided by .
Creating a dataset
We will show how to adjust the symbol size of the legend items in the case of a simple scatter plot. For that we create two random variables, one of the x axis and one for the y axis. Meanwhile each point will be colored based on a random categorical variable that well call category.
This gives the following data frame of random points distributed normally around 0 and categorized as or based on a coin toss.
Creating a scatter plot
Using our random dataset we can create a scatter plot with where:
- Each point is a pair of values .
- Each point color is based on the column.
- The color aesthetic gives rise to a legend where each legend item is a colored point.
We do that with the following code.
Which creates a scatter
You can use the following syntax to change the size of elements in a ggplot2 legend:
ggplot(data, aes(x=x, y=y)) + theme( = unit(1, 'cm'), #change legend key size = unit(1, 'cm'), #change legend key = unit(1, 'cm'), #change legend key width = element_text(size=14), #change legend title font = element_text(size=10)) #change legend text font sizeThe following examples show how to use these arguments in practice.
Change ggplot2 Legend Key Size
Suppose we create the following grouped barplot using ggplot2:
library(ggplot2) #create data frame df <- (team=rep(c('A', 'B', 'C'), each=3), position=rep(c('Guard', 'Forward', 'Center'), times=3), points=c(14, 8, 8, 16, 3, 7, 17, 22, 26)) #create grouped barplot ggplot(df, aes(fill=position, y=points, x=team)) + geom_bar(position='dodge', stat='identity')By default, ggplot2 provides a legend to the right of the graph.
The following code shows how to use the argument to make the keys of the legend larger:
ggplot(df, aes(fill=position, y=points, x=team)) + geom_bar(position='dodge', stat='identity') + theme( = unit(2, 'cm'))We can also use the and arguments to specify widths and he
Increase the horizontal space between legend keys with in . This argument takes a enhet object created with .
If you have a horizontal legend, generally placed on top or bottom of the plot with or , you can change the spacing between legend keys with . You can supply a enhet object to this argument, e.g. for 1 cm space between legend keys. See the documentation for for more options for units.
For lodrät legends changing changes the space between the legend title and the keys, but not between the keys, e.g. see the large space between the legend title and keys.
In beställning to change the space between the legend keys, you can first man the key size bigger with and then remove the grey background color with .
Note that the legend title is no longer aligned with the keys with this approach. You can also shift it over with the setting of .