-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy path2021-08-04-log-plot.Rmd
84 lines (56 loc) · 1.6 KB
/
2021-08-04-log-plot.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
---
description: How to make Log Plots in ggplot2 with Plotly.
name: Log Plots
permalink: ggplot2/log-plot/
thumnail_github: log-plot.png
layout: base
language: ggplot2
display_as: scientific
page_type: u-guide
order: 5
output:
html_document:
keep_md: true
---
```{r, echo = FALSE, message=FALSE}
knitr::opts_chunk$set(message = FALSE, warning=FALSE)
```
## Setting axis to log2 scale
```{r}
library(plotly)
library(ggplot2)
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
p <- ggplot(cars, aes(x = speed, y = dist)) +
geom_point()
p <- p + scale_x_continuous(trans = 'log2') +
scale_y_continuous(trans = 'log2')
ggplotly(p)
```
You can also format the axis ticks to be displayed as exponents.
```{r}
library(plotly)
library(ggplot2)
library(scales)
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
p <- ggplot(cars, aes(x = speed, y = dist)) +
geom_point()
p <- p + scale_x_continuous(trans = 'log2') +
scale_y_continuous(trans = 'log2')
p <- p + scale_y_continuous(trans = log2_trans(),
breaks = trans_breaks("log2", function(x) 2^x),
labels = trans_format("log2", math_format(2^.x)))
ggplotly(p)
```
<!--------------------- EXAMPLE BREAK ------------------------->
## Setting axis to log10 scale
```{r}
library(plotly)
library(ggplot2)
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
p <- ggplot(cars, aes(x = speed, y = dist)) +
geom_point()
p <- p + scale_y_log10()
ggplotly(p)
```
ternatively, you can use the function `scale_y_continuous(trans = "log10")`, which allows to transform breaks and the format of labels.
<!--------------------- EXAMPLE BREAK ------------------------->