Created
April 29, 2024 09:19
-
-
Save sebastiansauer/679dc2b111bec4b0db6504716517a9ab to your computer and use it in GitHub Desktop.
Zusammenhang zweier nominaler Variablen visualisieren mit ggplot, aufbereitet mit dplyr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
data(mtcars) | |
mtcars %>% | |
select(mpg, hp) %>% | |
slice_head(n = 5) | |
mtcars2 <- | |
mtcars %>% | |
select(mpg, hp) %>% | |
mutate(mpg_high = case_when( | |
mpg <= mean(mpg) ~ "0", | |
mpg > mean(mpg) ~ "1" | |
)) %>% | |
select(-mpg) | |
mtcars3 <- | |
mtcars2 |> | |
mutate(hp_high = case_when( | |
hp <= mean(hp) ~ "0", | |
hp > mean(hp) ~ "1" | |
)) |> | |
select(-hp) | |
mtcars3_filtered <- | |
mtcars3 %>% | |
filter(mpg_high == 1) | |
mtcars3_filtered | |
mtcars3_filtered %>% | |
filter(hp_high == 0) | |
mtcars3 %>% | |
select(hp_high, mpg_high) %>% | |
ggplot() + | |
aes(x = hp_high, fill = mpg_high) + | |
geom_bar(position = "fill") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment