Daten mit dplyr verknüpfen
Chris Cardillo
Data Scientist
batmobile_colors <- batmobile %>%
group_by(color_id) %>%
summarize(total = sum(quantity))
batmobile_colors
# A tibble: 12 x 2
color_id total
<dbl> <dbl>
1 0 543
2 1 33
3 4 16
4 14 20
5 15 16
6 36 15
7 57 8
8 71 202
9 72 160
10 182 8
# … mit 2 weiteren Zeilen
batwing_colors <- batwing %>%
group_by(color_id) %>%
summarize(total = sum(quantity))
batwing_colors
# A tibble: 20 x 2
color_id total
<dbl> <dbl>
1 0 418
2 1 45
3 4 81
4 14 22
5 15 22
6 19 10
7 25 1
8 34 3
9 36 9
10 46 21
# … mit 10 weiteren Zeilen
batmobile_colors %>%
full_join(batwing_colors, by = "color_id", suffix = c("_batmobile", "_batwing")) %>%
replace_na(list(total_batmobile = 0, total_batwing = 0))
# A tibble: 22 x 3
color_id total_batmobile total_batwing
<dbl> <dbl> <dbl>
1 0 543 418
2 1 33 45
3 4 16 81
4 14 20 22
5 15 16 22
6 36 15 9
7 57 8 3
8 71 202 158
9 72 160 213
10 182 8 14
# … mit 12 weiteren Zeilen
batmobile_colors %>%
full_join(batwing_colors, by = "color_id", suffix = c("_batmobile", "_batwing")) %>%
replace_na(list(total_batmobile = 0, total_batwing = 0)) %>%
inner_join(colors, by = c("color_id" = "id"))
# A tibble: 22 x 5
color_id total_batmobile total_batwing name rgb
<dbl> <dbl> <dbl> <chr> <chr>
1 0 543 418 Schwarz #05131D
2 1 33 45 Blau #0055BF
3 4 16 81 Rot #C91A09
4 14 20 22 Gelb #F2CD37
5 15 16 22 Weiß #FFFFFF
6 36 15 9 Transparent-Rot #C91A09
7 57 8 3 Transparent-Neonorange #FF800D
8 71 202 158 Hell-bläulich Grau #A0A5A9
9 72 160 213 Dunkel-bläulich Grau #6C6E68
10 182 8 14 Transparent-Orange #F08F1C
# … mit 12 weiteren Zeilen
batmobile_colors %>%
full_join(batwing_colors, by = "color_id", suffix = c("_batmobile", "_batwing")) %>%
replace_na(list(total_batmobile = 0, total_batwing = 0)) %>%
inner_join(colors, by = c("color_id" = "id")) %>%
mutate(total_batmobile = total_batmobile / sum(total_batmobile),
total_batwing = total_batwing / sum(total_batwing))
# A tibble: 22 x 5
color_id total_batmobile total_batwing name rgb
<dbl> <dbl> <dbl> <chr> <chr>
1 0 0.516 0.397 Schwarz #05131D
2 1 0.0314 0.0428 Blau #0055BF
3 4 0.0152 0.0770 Rot #C91A09
4 14 0.0190 0.0209 Gelb #F2CD37
5 15 0.0152 0.0209 Weiß #FFFFFF
6 36 0.0143 0.00856 Transparent-Rot #C91A09
7 57 0.00760 0.00285 Transparent-Neonorange #FF800D
8 71 0.192 0.150 Hell-bläulich Grau #A0A5A9
9 72 0.152 0.202 Dunkel-bläulich Grau #6C6E68
10 182 0.00760 0.0133 Transparent-Orange #F08F1C
# … mit 12 weiteren Zeilen
colors_joined <- batmobile_colors %>%
full_join(batwing_colors, by = "color_id", suffix = c("_batmobile", "_batwing")) %>%
replace_na(list(total_batmobile = 0, total_batwing = 0)) %>%
inner_join(colors, by = c("color_id" = "id")) %>%
mutate(total_batmobile = total_batmobile / sum(total_batmobile),
total_batwing = total_batwing / sum(total_batwing),
difference = total_batmobile - total_batwing)
colors_joined
# A tibble: 22 x 6
color_id total_batmobile total_batwing name rgb difference
<dbl> <dbl> <dbl> <chr> <chr> <dbl>
1 0 0.516 0.397 Schwarz #05131D 0.119
2 1 0.0314 0.0428 Blau #0055BF -0.0114
3 4 0.0152 0.0770 Rot #C91A09 -0.0618
4 14 0.0190 0.0209 Gelb #F2CD37 -0.00190
5 15 0.0152 0.0209 Weiß #FFFFFF -0.00570
6 36 0.0143 0.00856 Transparent-Rot #C91A09 0.00570
7 57 0.00760 0.00285 Transparent-Neonorange #FF800D 0.00475
8 71 0.192 0.150 Hell-bläulich Grau #A0A5A9 0.0418
9 72 0.152 0.202 Dunkel-bläulich Grau #6C6E68 -0.0504
10 182 0.00760 0.0133 Transparent-Orange #F08F1C -0.00570
# … mit 12 weiteren Zeilen
library(ggplot2)
library(forcats)
color_palette <- setNames(colors_joined$rgb, colors_joined$name)
colors_joined %>%
mutate(name = fct_reorder(name, difference)) %>%
ggplot(aes(name, difference, fill = name)) +
geom_col() +
coord_flip() +
scale_fill_manual(values = color_palette, guide = "none")


Daten mit dplyr verknüpfen