-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy path2021-08-04-distplot.Rmd
183 lines (130 loc) · 3.62 KB
/
2021-08-04-distplot.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
---
name: Distplots
permalink: ggplot2/distplot/
description: How to make Distplots in ggplot2 with Plotly.
layout: base
thumnail_github: distplot.png
language: ggplot2
page_type: u-guide
display_as: statistical
order: 4
output:
html_document:
keep_md: true
---
```{r, echo = FALSE, message=FALSE}
knitr::opts_chunk$set(message = FALSE, warning=FALSE)
```
### Basic Density Plot
```{r}
library(plotly)
library(ggplot2)
set.seed(1234)
dfGamma = data.frame(nu75 = rgamma(100, 0.75),
nu1 = rgamma(100, 1),
nu2 = rgamma(100, 2))
dfGamma = stack(dfGamma)
p <- ggplot(dfGamma, aes(x = values)) +
stat_density(aes(group = ind, color = ind),position="identity",geom="line")
fig <- ggplotly(p)
fig
```
### Density & Facet
```{r}
library(plotly)
require(plyr)
dd<-data.frame(matrix(rnorm(144, mean=2, sd=2),72,2),c(rep("A",24),rep("B",24),rep("C",24)))
colnames(dd) <- c("x_value", "Predicted_value", "State_CD")
dd <- data.frame(
predicted = rnorm(72, mean = 2, sd = 2),
state = rep(c("A", "B", "C"), each = 24)
)
grid <- with(dd, seq(min(predicted), max(predicted), length = 100))
normaldens <- ddply(dd, "state", function(df) {
data.frame(
predicted = grid,
density = dnorm(grid, mean(df$predicted), sd(df$predicted))
)
})
p <- ggplot(dd, aes(predicted)) +
geom_density() +
geom_line(aes(y = density), data = normaldens, colour = "red") +
facet_wrap(~ state)
fig <- ggplotly(p)
fig
```
### Multiple Density Plot
```{r}
library(plotly)
carrots <- data.frame(length = rnorm(100000, 6, 2))
cukes <- data.frame(length = rnorm(50000, 7, 2.5))
#Now, combine your two dataframes into one. First make a new column in each.
carrots$veg <- 'carrot'
cukes$veg <- 'cuke'
#and combine into your new data frame vegLengths
vegLengths <- rbind(carrots, cukes)
#now make your lovely plot
p <- ggplot(vegLengths, aes(length, fill = veg)) + geom_density(alpha = 0.2)
fig <- ggplotly(p)
fig
```
### Stacked Density Plot
```{r}
library(plotly)
set.seed(123)
df <- data.frame(x <- rchisq(1000, 5, 10),
group <- sample(LETTERS[1:5], size = 1000, replace = T))
p <- ggplot(df, aes(x, fill = group)) +
geom_density(alpha = 0.5, position = "stack") +
ggtitle("stacked density chart")
fig <- ggplotly(p)
fig
```
### Overlay Histogram
```{r}
library(plotly)
set.seed(123)
df <- data.frame(x <- rchisq(1000, 5, 10),
group <- sample(LETTERS[1:5], size = 1000, replace = T))
p <- ggplot(df, aes(x)) +
geom_histogram(aes(y = ..density..), alpha = 0.7, fill = "#333333") +
geom_density(fill = "#ff4d4d", alpha = 0.5) +
theme(panel.background = element_rect(fill = '#ffffff')) +
ggtitle("Density with Histogram overlay")
fig <- ggplotly(p)
fig
```
### Overlay Scatterplot
```{r}
library(plotly)
set.seed(123)
df <- data.frame(x <- rchisq(1000, 10, 10),
y <- rnorm(1000))
p <- ggplot(df, aes(x, y)) +
geom_point(alpha = 0.5) +
geom_density_2d() +
theme(panel.background = element_rect(fill = '#ffffff')) +
ggtitle("2D density plot with scatterplot overlay")
fig <- ggplotly(p)
fig
```
### Kernel Density Estimate
```{r}
library(plotly)
p <- ggplot(diamonds, aes(x = price)) +
geom_density(aes(fill = "epanechnikov"), kernel = "epanechnikov") +
facet_grid(~cut) +
ggtitle("Kernel density estimate with Facets")
fig <- ggplotly(p)
fig
```
### Kernel Density Plot
```{r}
library(plotly)
p <- ggplot(diamonds, aes(x = price)) +
geom_density(aes(fill = color), alpha = 0.5) +
ggtitle("Kernel Density estimates by group")
fig <- ggplotly(p)
fig
```
These plots were inspired by <a href="http://docs.ggplot2.org/current/">ggplot2 documentation</a>.