FAQ: How to order the (factor) variables in ggplot2

When you make a bar plot for categorical (i.e., factor) variables, probably you want to order the levels of variable in some way.

Continue reading

FAQ: geom_line() doesn’t draw lines

Probably one of the most frequently asked question is that geom_line() does not draw lines.
This is because x variable is factor.

Continue reading

Plot mts (multivariate time series) in ggplot2


ggplot2 does not support ts plot by default, but easily plot after brief data transformation.

Continue reading

Draw function without data in ggplot2

Sometimes I need to draw mathematical functions only by specifying the xlim and without read data.
stat_function() can handle this.


Basic form is:

qplot(c(0, 2), stat="function", fun=exp, geom="line") 
# or
ggplot(data.frame(x=c(0, 2)), aes(x)) + stat_function(fun=exp)

Continue reading