Skip to content

Instantly share code, notes, and snippets.

@saraemoore
Created March 25, 2014 17:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saraemoore/9766858 to your computer and use it in GitHub Desktop.
Save saraemoore/9766858 to your computer and use it in GitHub Desktop.
ggplot geom_boxplot with box fill and correctly spaced x-axis scale
library(ggplot2)
ggplot(data.df, aes(x=factor(time), y=value)) +
geom_boxplot(aes(fill=measure)) +
xlab("Time (time units)") + ylab("Value (value units)") +
scale_fill_discrete(name = "Measure")
ggplot(data.df, aes(x=time, y=value)) +
geom_boxplot(aes(fill=measure)) +
xlab("Time (time units)") + ylab("Value (value units)") +
scale_fill_discrete(name = "Measure")
ggplot(data.df, aes(x=time, y=value)) +
geom_boxplot(aes(fill=measure, group=factor(time))) +
xlab("Time (time units)") + ylab("Value (value units)") +
scale_fill_discrete(name = "Measure")
ggplot(data.df, aes(y=value, x=time)) +
geom_boxplot(aes(position = factor(time), fill=measure)) +
xlab("Time (time units)") + ylab("Value (value units)") +
scale_fill_discrete(name = "Measure")
ggplot(data.df, aes(y=value, x=time)) +
geom_boxplot(aes(position = factor(time), fill=measure)) +
scale_x_continuous(breaks = unique(data.df$time)) +
xlab("Time (time units)") + ylab("Value (value units)") +
scale_fill_discrete(name = "Measure")
data.df = data.frame(time = rep(c(0,3,6,12,24,48),each=50),
value = rnorm(300, 20, 5),
measure = factor(rep(rep(c("A","B"),each=25), 6)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment