library(dslabs)
library(tidyverse)── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.2.1 ✔ readr 2.2.0
✔ forcats 1.0.1 ✔ stringr 1.6.0
✔ ggplot2 4.0.3 ✔ tibble 3.3.1
✔ lubridate 1.9.5 ✔ tidyr 1.3.2
✔ purrr 1.2.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
view(relig_income)
data(relig_income)
relig_income_tidy = pivot_longer(relig_income, cols = -religion, names_to = "salary", values_to = "n")
print(relig_income_tidy, n = 50)# A tibble: 180 × 3
religion salary n
<chr> <chr> <dbl>
1 Agnostic <$10k 27
2 Agnostic $10-20k 34
3 Agnostic $20-30k 60
4 Agnostic $30-40k 81
5 Agnostic $40-50k 76
6 Agnostic $50-75k 137
7 Agnostic $75-100k 122
8 Agnostic $100-150k 109
9 Agnostic >150k 84
10 Agnostic Don't know/refused 96
11 Atheist <$10k 12
12 Atheist $10-20k 27
13 Atheist $20-30k 37
14 Atheist $30-40k 52
15 Atheist $40-50k 35
16 Atheist $50-75k 70
17 Atheist $75-100k 73
18 Atheist $100-150k 59
19 Atheist >150k 74
20 Atheist Don't know/refused 76
21 Buddhist <$10k 27
22 Buddhist $10-20k 21
23 Buddhist $20-30k 30
24 Buddhist $30-40k 34
25 Buddhist $40-50k 33
26 Buddhist $50-75k 58
27 Buddhist $75-100k 62
28 Buddhist $100-150k 39
29 Buddhist >150k 53
30 Buddhist Don't know/refused 54
31 Catholic <$10k 418
32 Catholic $10-20k 617
33 Catholic $20-30k 732
34 Catholic $30-40k 670
35 Catholic $40-50k 638
36 Catholic $50-75k 1116
37 Catholic $75-100k 949
38 Catholic $100-150k 792
39 Catholic >150k 633
40 Catholic Don't know/refused 1489
41 Don’t know/refused <$10k 15
42 Don’t know/refused $10-20k 14
43 Don’t know/refused $20-30k 15
44 Don’t know/refused $30-40k 11
45 Don’t know/refused $40-50k 10
46 Don’t know/refused $50-75k 35
47 Don’t know/refused $75-100k 21
48 Don’t know/refused $100-150k 17
49 Don’t know/refused >150k 18
50 Don’t know/refused Don't know/refused 116
# ℹ 130 more rows
library(ggiraph)
library(ggplot2)
library(dplyr)
library(patchwork)
#I modeled this after a graph i found here:https://r-graph-gallery.com/414-map-multiple-charts-in-ggiraph.html
relig_bar_chart <- ggplot(relig_income_tidy, aes(x = reorder(religion, n), y = n, fill = salary, tooltip =religion, data_id = religion)) +
geom_col_interactive() +
coord_flip()
relig_bar_chartrelig_bar_chart_interactive <- girafe(ggobj = relig_bar_chart)
relig_bar_chart_interactive#not sure if it's useful, but now it's interactive