Skip to content

Instantly share code, notes, and snippets.

@tomhopper
Created July 2, 2016 15:35
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 tomhopper/3be48cd03517481c96c8f5a6f8fd6c96 to your computer and use it in GitHub Desktop.
Save tomhopper/3be48cd03517481c96c8f5a6f8fd6c96 to your computer and use it in GitHub Desktop.
makeover: convert from two groups of side-by-side vertical bar charts to a more readable dot plot
# from Conrad Hacket
# Median hourly earnings
# \url{https://twitter.com/conradhackett/status/748884076493475840}
# makeover: convert from two groups of side-by-side vertical bar charts to a more readable dot plot
# Demonstrates:
# Use of in ggplot2
# Creating dot plots
# Combining color and shape in a single legend
# Sorting a dataframe so that categorical data in one column is ordered by a second numerical column
# Note: resulting graph displays best at about 450 pixels x 150 pixels
library(tidyr)
library(dplyr)
library(ggplot2)
shape_func <- function(x) {
y <- rep(15, times = length(x))
y[x == "Male"] <- 1
y[x == "Female"] <- 4
return(y)
}
earnings_df <- data_frame(gender = factor(rep(c("Male","Female"), each = 4)),
race = factor(rep(c("White","Black","Hispanic","Asian"), times = 2)),
earnings = c(21,15,14,24,
17,13,12,18)) %>%
mutate(race = reorder(race, earnings))
ggplot(earnings_df) +
aes(y = earnings, x = race) +
#geom_hline(yintercept = earnings_df$earnings[which(earnings_df$gender == "Male" & earnings_df$race == "White")],
# colour = "grey80", linetype = "dashed", alpha = 0.75) +
geom_point(aes(colour = gender, shape = gender), size = 2.5) +
scale_y_continuous(minor_breaks = seq(12, 24, by = 1)) +
scale_color_discrete(name = "Gender") +
scale_shape_manual(name = "Gender", values = c(4, 1)) +
coord_flip() +
ylab("Median Earnings per Hour ($/hr)") +
theme_bw() +
theme(axis.title.y = element_blank())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment