-
Notifications
You must be signed in to change notification settings - Fork 3
/
week-45.Rmd
186 lines (157 loc) · 5.23 KB
/
week-45.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
184
---
title: "week-45"
author: "Maxwel Coura Oliveira"
date: "11/2/2021"
output: html_document
---
```{r}
# Get the Data
library(spData)
# note that spDataLarge needs to be installed via:
# install.packages("spDataLarge",
# repos = "https://nowosad.github.io/drat/", type = "source")
library(spDataLarge)
library(sf)
library(raster)
library(tidyverse)
library(rvest)
```
```{r}
country_function <- function(id) {
url <- paste0("http://weedscience.org/Summary/Country.aspx?CountryID=",id,"")
#id will change by each country id number
# Read url
resistance <- read_html(url)
# Extract herbicide resistance data
chart <- resistance %>%
html_node(".rgMasterTable") %>% # selector
html_table(fill = TRUE) # get the table
# Tidy dataset
final_chart <- chart %>%
janitor::row_to_names(row_number = 2) %>% # make second column header
janitor::clean_names() %>% # clean header
as_tibble() %>% # tibble is better than data.frame
drop_na() %>% # drop NA values
mutate_at(c("number", "first_year",
"country_id", "resist_id"),
as.integer) # make columns numbers as integer
# Get final dataset
final_chart
}
```
```{r}
country_function(id = 45) |> # usa id
count(state_name) |>
rename(n_cases = n) -> n_cases
```
```{r}
country_function(id = 45) |> # usa id
count(state_name, common_name) |>
count(state_name) |>
rename(n_species = n) -> species_n
```
```{r}
country_function(id = 45) |> # usa id
count(state_name, site_of_action) |>
count(state_name) |>
rename(n_soa = n) -> soa_n
```
```{r}
library(geofacet)
library(ggtext)
library(biscale)
library(cowplot)
```
```{r}
n_cases |>
left_join(species_n, by = "state_name") |>
left_join(soa_n, by = "state_name") |>
add_row(state_name = "Puerto Rico", n_cases = 0, n_species = 0, n_soa = 0) |>
add_row(state_name = "Alaska", n_cases = 0, n_species = 0, n_soa = 0) |>
add_row(state_name = "Nevada", n_cases = 0, n_species = 0, n_soa = 0) |>
bi_class(x = n_cases, y = n_species, style = "quantile", dim = 3) -> dataset
```
```{r}
us_state_grid1 %>%
add_row(row = 7, col = 10, code = "PR", name = "Puerto Rico") -> us_state_grid1_2
```
```{r}
dataset |>
left_join(us_state_grid1_2,
by = c("state_name" = "name")) |>
mutate(ratio = round(n_cases / n_species, 2)) -> dataset1
```
```{r}
# Palette
custom_pal <- bi_pal_manual(val_3_1 = "#F46D43", val_3_2 = "#D73027", val_3_3 = "#A50026",
val_2_1 = "#D9EF8B", val_2_2 = "#FFFFBF",val_2_3 = "#FEE08B",
val_1_1 = "#006837", val_1_2 = "#1A9850",val_1_3 = "#66BD63")
(legend <- bi_legend(pal = custom_pal,
dim = 3,
xlab = "Higher number of cases",
ylab = "Higher number of species",
size = 8) +
bi_theme() +
theme(
rect = element_rect(fill = "grey10"),
panel.border = element_blank(),
axis.ticks = element_blank(),
axis.text = element_blank(),
axis.title.x = element_text(size = 9,
color = "#F46D43",
face = "bold"),
axis.title.y = element_text(size = 9,
color = "#66BD63",
face = "bold"),
plot.background = element_rect(fill = "#F9F6EE",
color = NA),
panel.background = element_rect(fill = "#F9F6EE",
color = NA))
)
```
```{r}
dataset1 %>%
ggplot() +
geom_rect(aes(fill = bi_class), xmin = -1, ymin = -1, xmax = 1, ymax = 1,
color = "white") +
geom_richtext(aes(label = code,
color = ifelse(str_detect(bi_class,"1-1|1-2|3-3"), "white", "#333333")),
hjust = 0.5,
size = 10,
x = 0, y = 0,
fontface = "bold",
fill = NA,
label.colour = NA,
show.legend = F) +
facet_geo(vars(state_name), grid = us_state_grid1_2) +
coord_fixed(xlim =c(-1,1),
ylim = c(-1,1)) +
scale_color_identity() +
bi_scale_fill(pal = custom_pal,
dim = 3 ,
guide = "none") +
theme_minimal() +
labs(
title = "Herbicide Weed Resistance in the US",
subtitle = "A bivariate map showing reported resistance cases vs resistant weed species",
caption = "**Source:** WeedScience.org | **Figure:** @maxwelco"
) +
theme(
panel.spacing = unit(-5,"points"),
strip.text = element_blank(),
plot.title = element_text(size = 30,
color = "#333333",
hjust = .5, margin = margin(t = 10, b = 15),
face = "bold"),
plot.subtitle = element_text(size = 15, hjust = 0.5),
plot.caption = element_markdown(size = 10,
hjust = 1,
color = "#333333"),
# plot.margin = margin(t = 20, r = 10, b =10, l = 10),
plot.background = element_rect(fill = "#F9F6EE", color = NA)
) -> map
ggdraw() +
draw_plot(map, 0, 0, 1, 1) +
draw_plot(legend, 0.1, 0.8, 0.2, 0.2)
ggsave("fig_45.png", width = 16, height = 9)
```