public
Created

Graphic Parameters (symbols, line types, and colors) for ggplot2

  • Download Gist
ggplot2Cheat.r
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
require(ggplot2)
require(grid)
 
theme_update(panel.background=theme_blank(),
panel.grid.major=theme_blank(),
panel.border=theme_blank())
 
#Borrowed (i.e. stollen) from http://research.stowers-institute.org/efg/R/Color/Chart/ColorChart.R
getColorHexAndDecimal <- function(color) {
if(is.na(color)) {
return(NA)
} else {
c <- col2rgb(color)
return(sprintf("#%02X%02X%02X %3d %3d %3d", c[1],c[2],c[3], c[1], c[2], c[3]))
}
}
 
#Symbols
ggplot(data=data.frame(x=c(0:25))) + geom_point(size=10, aes(x=x,y=x,shape=x)) +
facet_wrap(~ x, scales='free') + xlab('') + ylab('') +
scale_shape_identity() +
opts(axis.text.x=theme_blank(), axis.text.y=theme_blank(),
axis.ticks=theme_blank(), legend.position='none')
ggsave('symbols.png')
 
#Line types
ggplot(data=data.frame(x=c(1:6))) + geom_hline(size=2, aes(yintercept=x, linetype=x)) +
scale_linetype_identity() +
xlab(NULL) + ylab(NULL) + xlim(c(0,100)) +
opts(axis.text.x=theme_blank(), axis.ticks=theme_blank(), legend.position='none')
ggsave('linetypes.png', width=6.5, height=2)
 
#Colors
df = data.frame(x=rep(1:26, 26), y=rep(1:26, each=26))
df$c = NA
df[1:length(colors()),'c'] = colors()
df$n = NA
df[1:length(colors()),'n'] = 1:length(colors())
df$r = df$g = df$b = NA
df[1:length(colors()),c('r','g','b')] = t(col2rgb(colors()))
df$text = ifelse(apply(df[,c('r','g','b')], 1, sum) > (255*3/2), 'black', 'white')
df$hex = lapply(df$c, getColorHexAndDecimal)
df$hex2 = paste(format(df$n, width=3), format(df$c, width=(max(nchar(df$c))+1)), format(df$hex, width=(max(nchar(df$hex))+1)))
 
ggplot(df, aes(x=x, y=y, fill=c, label=n)) + geom_tile() + geom_text(aes(colour=text), size=3) +
scale_fill_identity() +
scale_colour_identity() +
xlab(NULL) + ylab(NULL) +
opts(axis.text.x=theme_blank(), axis.ticks=theme_blank(), plot.margin=unit(c(0,0,0,0), "cm"),
axis.text.y=theme_blank(), axis.ticks=theme_blank(), legend.position='none')
ggsave('colors.png')
 
ggplot(df[1:100,], aes(x=1, y=n, fill=c, label=hex2, colour=text)) +
geom_tile() + geom_text(family = 'mono') +
scale_fill_identity() +
scale_colour_identity() +
xlab(NULL) + ylab(NULL) +
opts(axis.text.x=theme_blank(), axis.ticks=theme_blank(), plot.margin=unit(c(0,0,0,0), "cm"),
axis.text.y=theme_blank(), axis.ticks=theme_blank(), legend.position='none')

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.