-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy path2021-08-04-legend.Rmd
162 lines (99 loc) · 3.26 KB
/
2021-08-04-legend.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
---
description: How to work with Legends in ggplot2 with Plotly.
name: Legends
permalink: ggplot2/legend/
thumnail_github: legends.gif
layout: base
language: ggplot2
display_as: file_settings
page_type: u-guide
order: 14
output:
html_document:
keep_md: true
---
```{r, echo = FALSE, message=FALSE}
knitr::opts_chunk$set(message = FALSE, warning=FALSE)
```
## Default box plot
```{r}
library(plotly)
library(ggplot2)
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
p <- ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) +
geom_boxplot()
ggplotly(p)
```
<!--------------------- EXAMPLE BREAK ------------------------->
## Change the legend position
The position of the legend can be changed using the function `theme()` as follow:
```{r}
library(plotly)
library(ggplot2)
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
p <- ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) +
geom_boxplot()
p <- p + theme(legend.position="bottom")
ggplotly(p)
```
Note that, the argument `legend.position` can be also a numeric vector `c(x,y)`. In this case it is possible to position the legend inside the plotting area. x and y are the coordinates of the legend box. Their values should be between 0 and 1. `c(0,0)` corresponds to the `bottom left` and `c(1,1)` corresponds to the `top right` position.
```{r}
library(plotly)
library(ggplot2)
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
p <- ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) +
geom_boxplot()
p <- p + theme(legend.position = c(0.8, 0.2))
ggplotly(p)
```
<!--------------------- EXAMPLE BREAK ------------------------->
## Change the legend title and text font styles
```{r}
library(plotly)
library(ggplot2)
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
p <- ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) +
geom_boxplot()
p <- p + theme(legend.title = element_text(colour="red", size=10,
face="italic"))
p <- p + theme(legend.text = element_text(colour="blue", size=10,
face="bold"))
ggplotly(p)
```
<!--------------------- EXAMPLE BREAK ------------------------->
## Change the background color of the legend box
```{r}
library(plotly)
library(ggplot2)
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
p <- ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) +
geom_boxplot()
p <- p + theme(legend.background = element_rect(fill="lightblue",
size=0.5, linetype="solid",
colour ="red"))
ggplotly(p)
```
<!--------------------- EXAMPLE BREAK ------------------------->
## Change the order of legend items
```{r}
library(plotly)
library(ggplot2)
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
p <- ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) +
geom_boxplot()
p <- p + scale_x_discrete(limits=c("2", "0.5", "1"))
ggplotly(p)
```
<!--------------------- EXAMPLE BREAK ------------------------->
## Remove the plot legend
```{r}
library(plotly)
library(ggplot2)
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
p <- ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) +
geom_boxplot()
p <- p + theme(legend.title = element_blank())
p <- p + theme(legend.position='none')
ggplotly(p)
```
<!--------------------- EXAMPLE BREAK ------------------------->