ggplot2

Those are the useful tips I have gathered from the R and ggplot2 mailing lists.

List

Change the default color and shape

Example illustrates selective genotyping strategy.

Y <- rnorm(300,0,10)
X <- 1:300
sel <- rep("Non-genotyped", 300)
index <- which(abs(Y) > 9)
sel[index] <- "Genotyped"
mydat2 <- data.frame(Y,X,sel)

# colors
qplot(X,Y, color=factor(sel),data=mydat2)+ scale_color_manual(value=c("red", "black")) + labs(color="") 
+ xlab("Individual") + ylab("Phenotype")

dev.new()

# shapes
qplot(X,Y, shape=factor(sel),data=mydat2)+ scale_shape_manual(value=c(16,1)) + labs(shape="") + xlab("Individual") + ylab("Phenotype")

Change the default size of points

p <- ggplot(mtcars, aes(wt, mpg))  + geom_point(aes(size = qsec)) 
p
dev.new()
p + scale_size(to=c(1,3))
dev.new()
p + scale_size(to=c(5,10)

Zoom-in

mydat <- data.frame(x = runif(100), y = rnorm(100))
qplot(x, y, data = mydat) + geom_smooth()
dev.new()
qplot(x, y, data = mydat) + geom_smooth() + coord_cartesian(xlim = c(.2, .8))

Use column index as aes

ggplot(mtcars, aes(wt, mpg)) + geom_point()
dev.new()
ggplot(mtcars, aes_string(x=paste(colnames(mtcars)[6]), y= paste(colnames(mtcars)[1]))) +geom_point()

Add labels to each data point

ggplot(mtcars, aes(x=hp, y=mpg, label=cyl))+geom_point()+geom_text()
ggplot(iris[sample(1:150, 10),  ], aes(x=Sepal.Width, y=Sepal.Length)) +geom_point() + geom_text(aes(x=Sepal.Width+0.20,label=Species)) 

Use every nth points for error bar

df <- data.frame(time=rep(1:100, 2),S=sin(rep(1:100, 2)*pi/50) + rnorm(200, 0, 0.1),Sex=gl(2,100),Err=rnorm(200,0,0.2))

head(df)
  time       S Sex       Err
1    1 0.10585   1  0.142666
2    2 0.18760   1 -0.027771
3    3 0.12801   1 -0.102478
4    4 0.36286   1  0.041794
5    5 0.32320   1 -0.035994
6    6 0.35872   1  0.118740

ggplot(df,aes(time,S,color=Sex)) + geom_point() + geom_errorbar(aes(x=time,ymin=S-Err,ymax=S+Err))
dev.new()
ggplot(df,aes(time,S,color=Sex)) + geom_point() + geom_errorbar(aes(x=time,ymin=S-Err,ymax=S+Err), data=df[seq(1,nrow(df), 10),])

Adjust number of axis tick

df <- data.frame(x = 1:400, y = runif(400))
ggplot(data = df, aes(x = x, y = y)) +  geom_point()
dev.new()
ggplot(data = df, aes(x = x, y = y)) +  geom_point() +  scale_x_continuous(breaks = seq(0,400,20))

# just add the first and the last ticks
ggplot(data = df, aes(x = x, y = y)) +  geom_point() +  scale_x_continuous(limits = c(0, 400), expand = c(0, 0))

Add a textbox

require(gridExtra)
p <- qplot(1:10,1:10, colour=1:10)
labs <- cbind(paste("hoge",1:7), paste("piyo",1:7))
g <- tableGrob(labs)
grid.arrange(p, g, nrow=1, widths=c(3,1))

Convex hulls with ggplot2

# ex1
df <- data.frame(x = rnorm(100), y = rnorm(100))
hull <- df[chull(df$x, df$y), ]
p <- ggplot(df, aes(x, y)) +geom_point() + geom_polygon(data = hull, fill = NA, colour = "grey50")

# ex2
df <- data.frame(x = rnorm(100), y = rnorm(100),z = sample(letters[1:5], 100, rep = T))
ggplot(df, aes(x, y, colour = z)) + geom_point()
find_hull <- function(df) df[chull(df$x, df$y), ]
hulls <- ddply(df, "z", find_hull)
ggplot(df, aes(x, y, colour = z)) + geom_point() + geom_polygon(data = hulls, fill = NA)

Align figures

Use faceting or grid.arrange() from the gridExtra package.

require(gridExtra)
p <- qplot(mpg, cyl, data=mtcars, geom="point")
q <- qplot(mpg, hp, data=mtcars, geom="point")
r <- qplot(mpg, wt, data=mtcars, geom="point")
s <- qplot(mpg, am, data=mtcars, geom="point")
grid.arrange(p,q,r,s,nrow=2)

Annotate a figure

ggplot(mydat, aes(x, y)) + geom_point() + annotate("text", x=0.8, y=0.2, label="hoge")

Annotate in facet_grid / facet_wrap figures

mydat <- data.frame(x=rexp(10),y=rnorm(10), z = c("a", "b"))
anno <- data.frame(x = c(0.6, 0.6), y = c(0.3,0.3), z = c("a", "b"), lab = c('hoge', 'piyo'))
ggplot(mydat, aes(x, y))+ geom_point() + facet_wrap(~z, scales="free") + geom_text(data = anno, aes(label = lab))



© 2010-2011 MOROTA, Gota