More Related Content
Similar to 好みの日本酒を呑みたい! 〜さけのわデータで探す自分好みの酒〜
Similar to 好みの日本酒を呑みたい! 〜さけのわデータで探す自分好みの酒〜 (20)
More from Takashi Kitano (11)
好みの日本酒を呑みたい! 〜さけのわデータで探す自分好みの酒〜
- 10. ## data.frame
brands.dist.df <-
brands.dist.mat %>%
as.matrix() %>%
# tibble
tibble::as_tibble(rownames = NA) %>%
#
tibble::rownames_to_column(var = "brandId1") %>%
#
tidyr::pivot_longer(
cols = -brandId1,
names_to = "brandId2",
values_to = "dist") %>%
#
dplyr::filter(brandId1 != brandId2) %>%
# ID
dplyr::mutate_if(is.character, as.integer)
- 16. n <- 6 # ( 6 )
brands.cluster <-
brands.dist.mat %>%
#
hclust(method="ward.D2") %>%
#
cutree(n) %>%
# tibble
tibble::tibble(
brandId = as.integer(names(.)),
cluster = .
)
- 18. radars <-
radars %>%
#
dplyr::group_by(cluster) %>%
tidyr::nest() %>%
dplyr::mutate(fig = purrr::map2(data, cluster, function(x, y) {
plotly::plot_ly(
type = "scatterpolar", mode = "markers",
r = c(x$f1, x$f2, x$f3, x$f4, x$f5, x$f6, x$f1),
theta = c(" ", " ", " ", " ", " ", " ", " "),
fill = 'toself',
fillcolor = RColorBrewer::brewer.pal(n = n, name = "Accent")[y],
opacity = 0.5
) %>%
plotly::layout(polar = list(angularaxis = list(
rotation = 90,
direction = 'counterclockwise')))
}))
- 25. brands.mds <-
brands.dist.mat %>%
# (MDS)
cmdscale() %>%
`colnames<-`(c("x", "y")) %>%
tibble::as_tibble(rownames = NA) %>%
tibble::rownames_to_column(var = "brandId") %>%
dplyr::mutate(brandId = as.integer(brandId)) %>%
#
dplyr::inner_join(
brands.cluster,
by = c("brandId" = "brandId")
) %>%
#
dplyr::inner_join(
brands[, -3],
by = c("brandId" = "id")
) %>%
dplyr::mutate(cluster = forcats::as_factor(cluster))
1
- 27. brands.mds %>%
#
dplyr::filter(brandId %in% rankings$overall$brandId) %>%
#
plotly::plot_ly(x = ~x, y = ~y) %>%
plotly::add_markers(
color = ~cluster,
text = ~name,
colors = RColorBrewer::brewer.pal(n = n, name = "Accent")) %>%
plotly::add_text(
text = ~name,
textposition = "top center"
) %>%
plotly::layout(showlegend = FALSE)
- 38. #
res.ca <-
FactoMineR::CA(contingency.table, graph = FALSE)
# tibble
tags.biplot <-
tibble::tibble(
type = "tag",
x = res.ca$row$coord[, 1],
y = res.ca$row$coord[, 2],
label = rownames(contingency.table)) %>%
dplyr::bind_rows(
tibble::tibble(
type = "cluster",
x = res.ca$col$coord[, 1],
y = res.ca$col$coord[, 2],
label = colnames(contingency.table))
)
- 39. tags.biplot %>%
plotly::plot_ly(x =~x, y =~y) %>%
plotly::add_markers(color = ~type,
colors = RColorBrewer::brewer.pal(3, "Set1")[1:2]) %>%
plotly::add_text(text = ~label, textposition = "top center") %>%
plotly::layout(showlegend = FALSE)
factoextra::fviz_ca_biplot(
res.ca,
font.family = "HiraKakuProN-W3"
)
- 42. wordclouds <-
brand_tags %>%
tidyr::unnest(cols = tagIds) %>%
dplyr::inner_join(
brands.cluster,
by = c("brandId" = "brandId")
) %>%
dplyr::inner_join(tags, by = c("tagIds" = "id")) %>%
#
dplyr::group_by(cluster, tag) %>%
dplyr::count() %>%
#
dplyr::group_by(cluster) %>%
dplyr::arrange(cluster, desc(n)) %>%
tidyr::nest() %>%
dplyr::mutate(fig = purrr::map(
data,
wordcloud2::wordcloud2, size = 0.8, minSize = 10
))