『データ可視化学入門』をRで書く 2.1 数量と図形の大きさを紐づける

2.1 数量と図形の大きさを紐づける

この記事はR言語 Advent Calendar 2023 シリーズ2の4日目の記事です。
qiita.com

2.1.1 図形の大きさと数量を紐づける

##### 2.1.1 #####
library(tidyverse)
library(ggpubr)

label <- c("支持する", "支持しない", "どちらとも\nいえない", "無回答")
data.frame(
  label=factor(label, levels = rev(label)),
  data=c(40, 32, 25, 3)
  ) |>
  ggdonutchart(
    x = "data",
    label = "label",
    fill = "label",
    palette = c("darkgray", "lightgray", "red", "blue")
    ) +
  theme(legend.position = "none")
2.1.1

2.1.2 ワードクラウドを用いた「頻度と見た目の大きさ」の紐づけ

##### 2.1.2 #####
library(tidyverse)
library(tidytext)
library(wordcloud2)
library(webshot)
library(htmlwidgets)

# テキストデータ
df <- data.frame(
  text = "Clustering coefficients for correlation networks, Energy landscape analysis of neuroimaging data, Simulation of space acquisition process of pedestrians using proxemic floor field model, Pedestrian flow through multiple bottlenecks, Closer to critical resting-state neural dynamics in individuals with higher fluid intelligence, Reinforcement learning explains conditional cooperation and its moody cousin, Jam-absorption driving with a car-following model, Age‐related changes in the ease of dynamical transitions in human brain activity, Potential global jamming transition in aviation networks, Methodology and theoretical basis of forward genetic screening for sleep/wakefulness in mice, Jamming transitions in force-based models for pedestrian dynamics, Inflow process of pedestrians to a confined space, Exact solution of a heterogeneous multilane asymmetric simple exclusion process, Exact stationary distribution of an asymmetric simple exclusion process with Langmuir kinetics and memory reservoirs, Taming macroscopic jamming in transportation networks, A demonstration experiment of a theory of jam-absorption driving, A balance network for the asymmetric simple exclusion process, Reinforcement learning account of network reciprocity, Towards understanding network topology and robustness of logistics systems, Inflow process: A counterpart of evacuation, Analysis on a single segment of evacuation network, Dynamics of assembly production flow, Presynaptic inhibition of dopamine neurons controls optimistic bias, Bridging the micro-macro gap between single-molecular behavior and bulk hydrolysis properties of cellulase, Cluster size distribution in 1D-CA traffic models, Modelling state‐transition dynamics in resting‐state brain signals by the hidden Markov and Gaussian mixture models, Positive congestion effect on a totally asymmetric simple exclusion process with an adsorption lane, Metastability in pedestrian evacuation, Constructing quantum dark solitons with stable scattering properties, The Autonomous Sensory Meridian Response Activates the Parasympathetic Nervous System, Trait, staging, and state markers of psychosis based on functional alteration of salience-related networks in the high-risk, first episode, and chronic stages, Critical brain dynamics and human intelligence, Influence of velocity variance of a single particle on cellular automaton models, Collective motion of oscillatory walkers, Reinforcing critical links for robust network logistics: A centrality measure for substitutability, Dynamic transitions between brain states predict auditory attentional fluctuations, Associations of conservatism/jumping to conclusions biases with aberrant salience and default mode network, Model retraining and information sharing in a supply chain with long-term fluctuating demands, Functional alterations of salience-related networks are associated with traits, staging, and the state of psychosis."
)

# ストップワードが違うようで、全く一緒にはならない
df |>
  unnest_tokens(word, text) |>
  count(word, sort = TRUE, name = "freq") |>
  anti_join(stop_words) |>
  wordcloud2(shape = "square", shuffle = FALSE)
2.1.2

2.1.3 ツリーマップによるグループ情報の付与

##### 2.1.3 #####
library(tidyverse)
library(treemapify)
library(gapminder)

# gapminderデータセットから2007年のデータを抽出
df <- gapminder |>
  dplyr::filter(year == 2007)

df |>
  ggplot(aes(area = pop, fill = lifeExp, label = country, subgroup = continent)) +
  geom_treemap() +
  geom_treemap_text(colour = "black", place = "topleft", reflow = TRUE) +
  geom_treemap_subgroup_border() +
  geom_treemap_subgroup_text(
    place = "centre", grow = TRUE, alpha = 0.6, colour = "black",
    fontface = "italic", min.size = 0
    ) +
  scale_fill_gradient2(
    low  = "red", mid  = "white", high = "blue", midpoint = mean(df$lifeExp)
    ) +
  theme(aspect.ratio = 1/2)
2.1.3

第2章1節は以上。