-
Notifications
You must be signed in to change notification settings - Fork 0
/
plotly-1.R
37 lines (37 loc) · 997 Bytes
/
plotly-1.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
# libraries ----
library(sf)
library(tidyverse)
library(plotly)
# load data ----
load(url("https://github.com/bariscr/data/raw/master/tr_sf.Rdata"))
# create the centroid ----
tr_center <- tr_sf %>%
st_centroid() %>%
mutate(
x = map_dbl(geometry, 1),
y = map_dbl(geometry, 2)
)
# Get Antalya label into the borders ----
tr_center$y[[which(tr_center$province_code == 7)]] <- 37.07
# create the plot ----
p <- plot_ly(tr_sf) %>%
add_sf(
color = ~goat,
split = ~province_name,
span = I(1),
text = ~paste(province_name, scales::comma(goat)),
hoveron = "fills",
hoverinfo = "text"
) %>%
layout(showlegend = FALSE) %>%
colorbar(title = "Number of Goats") %>%
add_annotations(
data = select(tr_center, province_name, x, y),
text = ~province_name, x = ~x, y = ~y,
showarrow = FALSE
)
# save as html ----
library(htmlwidgets)
library(plotly)
saveWidget(p, "p1.html", selfcontained = F, libdir = "lib")
zip("p2.zip", c("p1.html", "lib"))