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 let&#;s see how it works on an example.

The libraries

  • : For this particular situation you can customize the appearance of the items in a plot&#;s 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 we&#;ll 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

Sample data

In this tutorial we are going to use the following sample data categorized into two groups.

Adding a legend

If you want to add a legend to a ggplot2 chart you will need to pass a categorical (or numerical) variable to , , or inside . Depending on which argument you use to pass the data and your specific case the output will be different.

Color

Passing the categorical variable to inside the data will be plotted based on groups and an automatic legend will you up.

Note that this is the argument you should use if you want to create a plot that doesn’t admit a fill color, such as a scatter plot. You can pass a categorical or a numerical variable.

Alpha

Fill

If the plot you are creating allows adding a fill color you can use the argument inside , so the boxes of the legend will be colored.

Color and fill at the same time

In the previous scenario you can also use both arguments ( and ), so the legend will be a box filled with the correspoding color and the border will also have the corresponding color of the group.

If you want to change the fill or border colors of the chart (and automatically of the

  • ggplot teckenstorlek av legenden
  • 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 .