ggplot2: visualizing uncertainty

On Monday we thought about visualizing uncertainty. We produced the attached code Code-Day19.R.

useful geoms

ggplot provides a number of geoms specifically designed to estimate or visualize uncertainty. The include:

For details see: http://ggplot2.tidyverse.org/reference/

dat <- read.csv("http://unca-pols.org/Files/Data/Congress.csv")

dat$Chamber <- ifelse(dat$District.Num == 0, "Senate", "House")

dat$Error <- (dat$Dim1.SE + dat$Dim2.SE)/2

Senate2013 <- subset(dat, Chamber == "Senate" & Year == 2013 & Party != "Independent" & Name != "Obama")

ggplot(data = Senate2013, aes(x = Dim1, y = Dim2, color = Party)) +
	geom_point() +
	geom_point(aes(size = Error * 1.96), alpha = 0.1) +
	scale_size(name = "Error", range = c(2, 15)) +
	stat_ellipse(geom = "path", 
		level = 0.99, linetype = "dotted"
	) +
	stat_ellipse(geom = "path", 
		level = 0.95, linetype = "dashed"
	) +
	stat_ellipse(geom = "path", 
		level = 0.90, linetype = "solid"
	) +
	stat_ellipse(geom = "polygon", 
		level = 0.50, alpha = 0.1, 
		aes(fill = Party)
	) +
	scale_color_manual(values = c("#268bd2", "#dc322f")) +
	scale_fill_manual(values = c("#268bd2", "#dc322f")) +
	coord_cartesian(xlim = c(-1,1), ylim = c(-1,1)) +
	labs(x = "Dimension 1 Ideology", y = "Dimension 2 Ideology") +
	theme_solarized_2()