-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy path2021-08-04-axes.Rmd
193 lines (127 loc) · 3.76 KB
/
2021-08-04-axes.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
---
description: How to work with Axes in ggplot2 with Plotly.
name: Axes
permalink: ggplot2/axes/
thumnail_github: axes.png
layout: base
language: ggplot2
display_as: file_settings
page_type: u-guide
order: 13
output:
html_document:
keep_md: true
---
```{r, echo = FALSE, message=FALSE}
knitr::opts_chunk$set(message = FALSE, warning=FALSE)
```
## Default plot
```{r}
library(plotly)
library(ggplot2)
p <- ggplot(mtcars) +
geom_point(aes(disp, mpg)) +
scale_x_continuous(name = "Displacement")
ggplotly(p)
```
<!--------------------- EXAMPLE BREAK ------------------------->
## Changing axis limits
```{r}
library(plotly)
library(ggplot2)
p <- ggplot(mtcars) +
geom_point(aes(disp, mpg)) +
scale_x_continuous(limits = c(0, 600))
ggplotly(p)
```
<!--------------------- EXAMPLE BREAK ------------------------->
## Changing axis ticks
The `breaks` argument is used to specify where the ticks appear. It takes a numeric vector equal to the length of the number of ticks.
```{r}
library(plotly)
library(ggplot2)
p <- ggplot(mtcars) +
geom_point(aes(disp, mpg)) +
scale_x_continuous(breaks = c(150, 300, 450))
ggplotly(p)
```
<!--------------------- EXAMPLE BREAK ------------------------->
## Changing tick labels
You can change the tick labels using the `labels` argument. In the below example, we use words instead of numbers. When adding labels, we need to ensure that the length of the `breaks` and `labels` are same.
```{r}
library(plotly)
library(ggplot2)
p <- ggplot(mtcars) +
geom_point(aes(disp, mpg)) +
scale_x_continuous(breaks = c(150, 300, 450),
labels = c('One Hundred Fifty', 'Three Hundred', 'Four Hundred Fifity'))
ggplotly(p)
```
<!--------------------- EXAMPLE BREAK ------------------------->
## Changing position of labels
```{r}
library(ggplot2)
p <- ggplot(mtcars) +
geom_point(aes(disp, mpg)) +
scale_x_continuous(position = 'top')
ggplotly(p)
```
<!--------------------- EXAMPLE BREAK ------------------------->
## Y-axis example
Everything above using `scale_x_continuous()` can be used in the same way for y axis with `scale_y_continuous()`.
```{r}
library(plotly)
library(ggplot2)
p <- ggplot(mtcars) + geom_point(aes(disp, mpg)) +
scale_y_continuous(name = "Miles Per Gallon", limits = c(0, 45),
breaks = c(0, 15, 30, 45), position = 'right',
labels = c('zero', 'fifteen', 'thirty', 'fourtyfive'))
ggplotly(p)
```
<!--------------------- EXAMPLE BREAK ------------------------->
## Discrete axis labels
```{r}
library(plotly)
library(ggplot2)
p <- ggplot(mtcars) +
geom_bar(aes(factor(cyl))) +
scale_x_discrete(labels = c("4" = "Four", "6" = "Six", "8" = "Eight"))
ggplotly(p)
```
<!--------------------- EXAMPLE BREAK ------------------------->
## Remove Axis Labels
```{r}
library(plotly)
library(ggplot2)
df <- diamonds[sample(1:nrow(diamonds), size = 1000),]
p <- ggplot(df, aes(carat, price, color = cut)) +
geom_point() +
theme(axis.text = element_blank())
ggplotly(p)
```
## Vertical Text Orientation
```{r}
library(plotly)
library(ggplot2)
lab <- paste("Vertical Label", c(1, 2, 3, 4, 5))
ds <- data.frame(x = sample(lab, size = 1000, replace = T),
y = sample(LETTERS[1:5], size = 1000, replace = T))
p <- ggplot(ds, aes(x = x, fill = y)) +
geom_bar() +
theme(axis.text.x = element_text(angle = 90)) +
ggtitle("Vertical Axis Labels")
ggplotly(p)
```
## Angled Text Orientation
```{r}
library(plotly)
library(ggplot2)
lab <- paste("Angle Label", c(1, 2, 3, 4, 5))
ds <- data.frame(x = sample(lab, size = 1000, replace = T),
y = sample(LETTERS[1:5], size = 1000, replace = T))
p <- ggplot(ds, aes(x = x, fill = y)) +
geom_bar() +
theme(axis.text.x = element_text(angle = 45)) +
ggtitle("Angle Axis Labels")
ggplotly(p)
```