Skip to content

Instantly share code, notes, and snippets.

@cdesante
Created July 6, 2012 15:21
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 cdesante/3060834 to your computer and use it in GitHub Desktop.
Save cdesante/3060834 to your computer and use it in GitHub Desktop.
Time Series plots with custom breaks and colors
#by Christopher DeSante#
#Load ggplot2 #
library(ggplot2)
### X-axis: ###
anes.years <- c(1988, 1990, 1992, 1994, 2000, 2004, 2008,2016)
## Some DVs ##
mum1<-rnorm(8, 8,1)
mum2<-rnorm(8, 8,1)
mum3<-rnorm(8, 8,1)
mum4<-rnorm(8, 8,1)
#Combine into single vector: #
q.means <- c(mum1, mum2, mum3, mum4)
## Add text labels for four Questions ##
q.labels <- c(rep("Question 1...", 8),
rep("Question 2...", 8),
rep("Question 3...", 8),
rep("Question 4...", 8))
#Specify custom colours#
my.colors <- c(rep("darkred", 8), rep("purple4", 8),
rep( "darkgreen", 8), rep("steelblue4",8))
#begin plot command
qplot(anes.years,
q.means,
colour=my.colors,
geom="linerange",
ymin=0, ymax=q.means,
size=I(9),
ylim=c(0,20)
, xlab="\n \n Year", #\n adds break
ylab="Mean response \n"
) + facet_wrap(~q.labels) +
scale_x_continuous(limits=c(1988, 2016), breaks=anes.years) + # custom X ticks
scale_colour_identity() + # use custom colours
theme_bw( ) + # get rid of default grey background
opts(legend.position="none") + # hide legend
opts(axis.text.x = theme_text(angle = 45, hjust = 1, #customize X axis text
face="bold", colour= "black", size=12) ) +
opts(strip.text.x = theme_text(colour= "black", #customize Question Label text
size=16))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment