-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraficos.R
155 lines (131 loc) · 4.64 KB
/
graficos.R
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
# COMPRESSÃO - 3
compressao <- read.table(
"C:/Users/Annie de Lima/Downloads/Estatística/stat_debs/compressao.txt",
header = T)
names <- compressao$especie
value <- compressao$MOR2
data <- data.frame(names,value)
# Plot
data %>%
ggplot( aes(x=names, y=value, fill=names)) +
geom_boxplot() +
scale_fill_viridis(discrete = TRUE) +
theme_ipsum() +
theme(
legend.position="none",
plot.title = element_text(size=11)
) +
ggtitle("Compressão") +
ylab("MOR (MPa)") + ylim(200,750)
# Plot better results
data %>%
ggplot( aes(x=names, y=value, fill=names)) +
geom_boxplot() +
scale_fill_viridis(discrete = TRUE) +
geom_jitter(color="red", size=1.0, alpha=0.5) +
theme_ipsum() +
theme(
legend.position="none",
plot.title = element_text(size=11)
) +
ggtitle("Compressão") +
ylab("MOR (MPa)") + ylim(200,750)
# sample size
sample_size = data %>% group_by(names) %>% summarize(num=n())
# Plot
data %>%
left_join(sample_size) %>%
mutate(myaxis = paste0(names, "\n", "n=", num)) %>%
ggplot( aes(x=myaxis, y=value, fill=names)) +
geom_violin(width=1.4) +
geom_boxplot(width=0.1, color="red", alpha=0.2) +
scale_fill_viridis(discrete = TRUE) +
theme_ipsum() +
theme(
legend.position="none",
plot.title = element_text(size=12)
) +
ggtitle("Compressão") +
ylab("MOR (MPa)") + ylim(200,750)
# Code coming from @drob:
# https://gist.github.com/dgrtwo/eb7750e74997891d7c20#file-geom_flat_violin-r
"%||%" <- function(a, b) {
if (!is.null(a)) a else b
}
geom_flat_violin <- function(mapping = NULL, data = NULL, stat = "ydensity",
position = "dodge", trim = TRUE, scale = "area",
show.legend = NA, inherit.aes = TRUE, ...) {
layer(
data = data,
mapping = mapping,
stat = stat,
geom = GeomFlatViolin,
position = position,
show.legend = show.legend,
inherit.aes = inherit.aes,
params = list(
trim = trim,
scale = scale,
...
)
)
}
#' @rdname ggplot2-ggproto
#' @format NULL
#' @usage NULL
#' @export
GeomFlatViolin <-
ggproto("GeomFlatViolin", Geom,
setup_data = function(data, params) {
data$width <- data$width %||%
params$width %||% (resolution(data$x, FALSE) * 0.9)
# ymin, ymax, xmin, and xmax define the bounding rectangle for each group
data %>%
group_by(group) %>%
mutate(ymin = min(y),
ymax = max(y),
xmin = x,
xmax = x + width / 2)
},
draw_group = function(data, panel_scales, coord) {
# Find the points for the line to go all the way around
data <- transform(data, xminv = x,
xmaxv = x + violinwidth * (xmax - x))
# Make sure it's sorted properly to draw the outline
newdata <- rbind(plyr::arrange(transform(data, x = xminv), y),
plyr::arrange(transform(data, x = xmaxv), -y))
# Close the polygon: set first and last point the same
# Needed for coord_polar and such
newdata <- rbind(newdata, newdata[1,])
ggplot2:::ggname("geom_flat_violin",
GeomPolygon$draw_panel
(newdata, panel_scales, coord))
},
draw_key = draw_key_polygon,
default_aes = aes(weight = 1,
colour = "grey20",
fill = "white", size = 0.5,
alpha = NA, linetype = "solid"),
required_aes = c("x", "y")
)
# Final plot inspired from @jbburant: https://gist.github.com/jbburant/b3bd4961f3f5b03aeb542ed33a8fe062
data %>%
sample_frac(0.4) %>%
ggplot(aes(x = names, y = value, fill = names)) +
geom_flat_violin(scale = "count", trim = FALSE, width=2) +
scale_fill_viridis(discrete = TRUE) +
stat_summary(fun.data = mean_sdl,
fun.args = list(mult = 1),
geom = "pointrange",
position = position_nudge(4.9)) +
geom_dotplot(binaxis = "y",
dotsize = 1.2,
stackdir = "down",
binwidth = 0.3,
position = position_nudge(-0.025)) +
theme_ipsum() +
theme(
legend.position = "none"
) +
ylab("MOR (MPa)")
# Ver: https://www.data-to-viz.com/caveat/boxplot.html para explicações