Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Created May 15, 2019 13:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanburge/8d504b167642cef6218a47a8e2cd923a to your computer and use it in GitHub Desktop.
Save ryanburge/8d504b167642cef6218a47a8e2cd923a to your computer and use it in GitHub Desktop.
Standard Deviation Nones
rnorm2 <- function(n,mean,sd) { mean+sd*scale(rnorm(n)) }
whtevan <- rnorm2(100000, 5.13, 1.99)
oevan <- rnorm2(100000, 3.16, 2.13)
whtml <- rnorm2(100000, 4.21, 2.28)
bprot <- rnorm2(100000, 2.00, 1.56)
whtcath <- rnorm2(100000, 4.20, 2.24)
ocath <- rnorm2(100000, 3.01, 2.01)
ath <- rnorm2(100000, 2.42, 1.63)
agn <- rnorm2(100000, 2.730, 1.74)
nip <- rnorm2(100000, 3.37, 1.96)
mormon <- rnorm2(100000, 4.99, 1.87)
muslim <- rnorm2(100000, 2.62, 1.69)
jew <- rnorm2(100000, 2.95, 2.17)
buddhist <- rnorm2(100000, 2.76, 1.72)
hindu <- rnorm2(100000, 3.06, 1.91)
all <- rnorm2(100000, 3.73, 2.19)
full <- tibble(whtevan, oevan, whtml, bprot, whtcath, ocath, ath, agn, nip, mormon, muslim, jew, buddhist, hindu, all)
full <- melt(full) %>% as_tibble()
full <- full %>%
mutate(variable1 = frcode(variable == "whtml" ~ "White Mainline",
variable == "whtcath" ~ "White Catholic",
variable == "all" ~ "Entire Sample",
variable == "jew" ~ "Jewish",
variable == "oevan" ~ "Non-White Evangelical",
variable == "ocath" ~ "Non-White Catholic",
variable == "whtevan" ~ "White Evangelical",
variable == "nip" ~ "Nothing in Particular",
variable == "hindu" ~ "Hindu",
variable == "mormon" ~ "Mormon",
variable == "agn" ~ "Agnostic",
variable == "buddhist" ~ "Buddhist",
variable == "muslim" ~ "Muslim",
variable == "ath" ~ "Atheist",
variable == "bprot" ~ "Black Protestant"
))
full <- full %>%
mutate(sd = frcode(variable == "whtml" ~ "SD: 2.28",
variable == "whtcath" ~ "SD: 2.24",
variable == "all" ~ "SD: 2.19",
variable == "jew" ~ "SD: 2.17",
variable == "oevan" ~ "SD: 2.13",
variable == "ocath" ~ "SD: 2.01",
variable == "whtevan" ~ "SD: 1.99",
variable == "nip" ~ "SD: 1.96",
variable == "hindu" ~ "SD: 1.91",
variable == "mormon" ~ "SD: 1.87",
variable == "agn" ~ "SD: 1.74",
variable == "buddhist" ~ "SD: 1.72",
variable == "muslim" ~ "SD: 1.69",
variable == "ath" ~ "SD: 1.63",
variable == "bprot" ~ "SD: 1.56"
))
full <- full %>%
mutate(mean = frcode( variable == "whtml" ~ 4.21,
variable == "whtcath" ~ 4.2,
variable == "all" ~ 3.73,
variable == "jew" ~ 2.95,
variable == "oevan" ~ 3.16,
variable == "ocath" ~ 3.01,
variable == "whtevan" ~ 5.13,
variable == "nip" ~ 3.37,
variable == "hindu" ~ 3.06,
variable == "mormon" ~ 4.99,
variable == "agn" ~ 2.73,
variable == "buddhist" ~ 2.76,
variable == "muslim" ~ 2.62,
variable == "ath" ~ 2.42,
variable == "bprot" ~ 2.00
))
means <- full %>%
distinct(variable1, mean) %>%
mutate(mean = as.numeric(as.character(mean)))
labels <- full %>%
distinct(variable1, sd)
full <- full %>%
mutate(sd1= as.character(sd))
full$sd1 <- parse_number(full$sd1)
full %>%
ggplot(aes(value)) +
geom_density(aes(fill = variable1), alpha = .7) +
facet_wrap(~ variable1) +
theme_gg("Abel") +
scale_x_continuous(limits = c(1, 7), breaks = c(1,2,3,4,5,6,7), labels = c("Dem", "", "", "Ind", "", "", "Rep")) +
scale_fill_d3(palette = c("category20")) +
scale_y_continuous(labels = percent) +
theme(panel.spacing = unit(1, "lines")) +
theme(strip.text.x = element_text(size = 9)) +
theme(axis.text.y = element_text(size = 8)) +
geom_vline(data = means,aes(xintercept = mean),color = "black", linetype = "dashed") +
labs(x = "Party Identification", y = "", title = "Visualizing the Heterogeneity of Party Identification", caption = "@ryanburge\nData: CCES 2018", subtitle = "Mean Denoted by Dashed Vertical Line") +
geom_text(data = labels, aes(label=sd), x = Inf, y = -Inf, hjust=1.1, vjust=-12, inherit.aes = FALSE, family = "font", size = 3) +
ggsave("E://distribution_sd_pid7.png", type = "cairo-png", height = 8)
big <- rnorm2(100000, 45, 20)
med <- rnorm2(100000, 45, 10)
small <- rnorm2(100000, 45, 2)
full <- tibble(big, med, small)
full <- melt(full) %>% as_tibble()
full <- full %>%
mutate(variable1 = frcode(variable == "big" ~ "Large Std. Dev.",
variable == "med" ~ "Medium Std. Dev.",
variable == "small" ~ "Small Std. Dev."))
full <- full %>%
mutate(sd = frcode(variable == "big" ~ "SD: 20",
variable == "med" ~ "SD: 10",
variable == "small" ~ "SD: 2"))
labels <- full %>%
distinct(variable1, sd)
full %>%
ggplot(aes(value)) +
geom_density(aes(fill = variable1), alpha = .4) +
facet_wrap(~ variable1) +
theme_gg("Abel") +
scale_x_continuous(limits = c(0, 100)) +
scale_fill_d3(palette = c("category20")) +
scale_y_continuous(labels = percent) +
theme(panel.spacing = unit(1, "lines")) +
theme(strip.text.x = element_text(size = 10)) +
theme(axis.text.y = element_text(size = 8)) +
labs(x = "Age", y = "", title = "Visualizing Three Levels of Standard Deviation", caption = "@ryanburge\nData: Hypothetical Data") +
geom_text(data = labels, aes(label=sd), x = Inf, y = -Inf, hjust=2, vjust=-9, inherit.aes = FALSE, family = "font", size = 3) +
ggsave("E://distribution_sd_example.png", type = "cairo-png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment