-
Notifications
You must be signed in to change notification settings - Fork 49
/
2021-08-04-setting-graph-size.Rmd
95 lines (70 loc) · 1.78 KB
/
2021-08-04-setting-graph-size.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
---
description: How to Set Graph Size in ggplot2 with Plotly.
name: Setting Graph Size
permalink: ggplot2/setting-graph-size/
thumnail_github: setting-graph-size.png
layout: base
language: ggplot2
display_as: file_settings
page_type: u-guide
order: 10
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(mpg, aes(displ, hwy)) + geom_point()+
theme(
plot.margin = margin(1, 1, 1, 1, "cm"),
panel.background = element_rect(fill = "white"),
plot.background = element_rect(
fill = "grey90",
colour = "black"
)
)
ggplotly(p)
```
<!--------------------- EXAMPLE BREAK ------------------------->
## Add margin
To add margin use `plot.margin()`.
For the argument you can either use `margin(2, 2, 2, 2, "cm")` or `unit(c(2,2,2,2), "cm")`.
This two arguments are shorthand for `margin`:
* `t` = 2 - top
* `r` = 2 - right
* `b` = 2 - bottom
* `l` = 2 - left
* `unit ` = "cm"
```{r}
library(plotly)
library(ggplot2)
p <- ggplot(mpg, aes(displ, hwy)) + geom_point()+
theme(
plot.margin = margin(1, 1, 2, 2, "cm")
)
ggplotly(p)
```
<!--------------------- EXAMPLE BREAK ------------------------->
## Change background colour
To change the colour of the plot background `panel.background()`.
To change the colour of the background around the plot use `plot.background()`.
```{r}
library(plotly)
library(ggplot2)
p <- ggplot(mpg, aes(displ, hwy)) + geom_point()+
theme(
plot.margin = margin(1, 1, 1, 1, "cm"),
panel.background = element_rect(fill = "white"),
plot.background = element_rect(
fill = "grey90",
colour = "black"
)
)
ggplotly(p)
```
<!--------------------- EXAMPLE BREAK ------------------------->