Business professionals discussing data charts and graphs in a modern office setting.

The best way to Take away a Legend in ggplot2 (With Examples)

Posted on
banner 336x280

You’ll be able to usefulness please see syntax to take away a legend from a plot in ggplot2:

ggplot(df, aes(x=x, y=y, colour=z)) +
  geom_point() +
  theme(legend.place="none")

By means of specifying legend.place=”none” you’re telling ggplot2 to take away all legends from the plot.

banner 468x60

Refer to step by step instance displays methods to usefulness this syntax in apply.

Step 1: Develop the Knowledge Body

First, let’s build an information body:

#build information body
df <- information.body(assists=c(3, 4, 4, 3, 1, 5, 6, 7, 9),
                 issues=c(14, 8, 8, 16, 3, 7, 17, 22, 26),
                 place=rep(c('Shield', 'Ahead', 'Middle'), instances=3))

#view information body
df

  assists issues place
1       3     14    Shield
2       4      8  Ahead
3       4      8   Middle
4       3     16    Shield
5       1      3  Ahead
6       5      7   Middle
7       6     17    Shield
8       7     22  Ahead
9       9     26   Middle

Step 2: Develop a Plot The usage of ggplot2

Later, let’s usefulness ggplot2 to build a easy scatterplot:

library(ggplot2)

#build scatterplot
ggplot(df, aes(x=assists, y=issues, colour=place)) +
  geom_point(dimension=3)

By means of default, ggplot2 features a legend in order that it’s more straightforward to interpret the colours within the scatterplot.

Step 3: Take away the Legend from the Plot

Later, let’s usefulness legend.place=”none” to take away the legend from the plot:

library(ggplot2)

#build scatterplot with out a legend
ggplot(df, aes(x=assists, y=issues, colour=place)) +
  geom_point(dimension=3) +
  theme(legend.place="none")

The legend has been totally got rid of from the plot.

Homogeneous: The best way to Trade Legend Labels in ggplot2

Supplementary Sources

Refer to tutorials give an explanation for methods to carry out alternative usual operations in ggplot2:

The best way to Trade the Legend Name in ggplot2
The best way to Trade Legend Measurement in ggplot2
The best way to Trade Legend Place in ggplot2

banner 336x280

Leave a Reply

Your email address will not be published. Required fields are marked *