Skip to content

Instantly share code, notes, and snippets.

@pmallory
Created September 26, 2014 20:48
Show Gist options
  • Save pmallory/8f919bb007e45fd04634 to your computer and use it in GitHub Desktop.
Save pmallory/8f919bb007e45fd04634 to your computer and use it in GitHub Desktop.
A scatter plot with vertical positioning based on multiple categories
red_wine$volatile.acidity.cat <-
cut(red_wine$volatile.acidity,
breaks = quantile(red_wine$volatile.acidity,
probs = c(0, 0.25, 0.5, 0.75, 1.0)))
x_title = "Alcohol (percentage by volume)"
y_title = "Quality\n(0: very bad -> 10: very excellent)"
color_title = expression(paste("Volatile Acidity (", g / dm^{3}, ")"))
# Create label array for color legend.
# Note: Qi = ith Quartile.
l <- levels(red_wine$volatile.acidity.cat)
color_labels = c(paste("Q1:", l[1]), paste("Q2:", l[2]),
paste("Q3:", l[3]), paste("Q4:", l[4]))
red_wine$volatile.acidity.voffset <- red_wine$volatile.acidity.cat
levels(red_wine$volatile.acidity.voffset) <- c(-0.3, -0.1, 0.1, 0.3)
as.numeric.factor <- function(x) {as.numeric(levels(x))[x]}
#I added 0.05 of jitter on the alcohol axis because that quantity is only
#measured to the nearest tenth of a percent.
philipplot1 <- ggplot(data = red_wine,
aes(x = alcohol,
y = quality + as.numeric.factor(volatile.acidity.voffset),
color = volatile.acidity.cat)) +
geom_point(size = 1.8, position = position_jitter(w=0.05)) +
geom_hline(yintercept=3.5) +
geom_hline(yintercept=4.5) +
geom_hline(yintercept=5.5) +
geom_hline(yintercept=6.5) +
geom_hline(yintercept=7.5) +
scale_x_continuous(name = x_title) +
scale_y_continuous(name = y_title) +
scale_color_hue(name = color_title, labels = color_labels)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment