-
Notifications
You must be signed in to change notification settings - Fork 11
/
06-plots-basics.Rmd
56 lines (40 loc) · 1.35 KB
/
06-plots-basics.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
# Figures & Plots
In this section, basic useage of the `ggplot2` package is covered. Common clinical trial displays are then created and customized in a step wise manner.
## Basics
### Building a plot using ggplot2
#### Packages and Sample Data
```{r, echo = TRUE, message = FALSE, warning = FALSE}
# Packages
library(haven)
library(ggplot2)
# Data
adam_path <- "https://github.com/phuse-org/TestDataFactory/raw/main/Updated/TDF_ADaM/"
adsl <- as.data.frame(haven::read_xpt(paste0(adam_path, "adsl.xpt")))
```
Let's create a basic scatter plot of weight vs height.
```{r, eval = T}
# basic scatter plot of weight vs height
my_scatter_plot <- ggplot(adsl, aes(x = HEIGHTBL, y = WEIGHTBL)) +
geom_point()
my_scatter_plot
```
### Exporting plots
You may export plots as png files a few ways. If your plot is created in ggplot2, you can use the `ggsave()` function to export as png:
```{r, eval = F}
# save plot as a png file
ggsave(plot = my_scatter_plot,
filename = "my_plot_output_1.png",
width = 7,
height = 5,
units = "in")
```
The more general way to save plots as png files is to use the `png()` function. It can work with ggplot2, but also plots created in base R.
```{r, eval = F}
png(filename = "my_plot_output_2.png",
width = 7,
height = 5,
units = "in",
res = 300)
print(my_scatter_plot)
dev.off()
```