Monday, August 29, 2011

Ggplot2 Panels

I've been trying to use R to print out columns of data each in their own panel form for some time now. They all use the same time frame, of course, so I wanted to arrange by X-axis.

ggplot2 has a "facet" grammar you can use to plot different aspects of your data in a meaningful way -- i.e., different parts of an observation. The trick is to contort your data with the "melt" function, which will create two new variables per observation.

The first variable is the name of one of the columns.
The second variable is the value that corresponds to said variable.

I tried taking a peek at the data length -- observations should increase by a factor equal to the number of columns you are melting, i.e. if I'm melting by 3 variables


>>> data = melt(factor.data, measure.vars=c("variable_one", "variable_two", "variable_three"))
>>> length(factor.data$val)
85
>>> length(melted.data$val)
340


The data length increases by 3. But of course.

Here's the code I'm using to format, melt, plot, and save the data to pdf:


process.open <- function(filename) {
return(read.csv(file=filename, head=TRUE, sep=",", dec="."))
}

filename <- "z1_output_quint.csv"
output <- "z1_output_quint_panel.pdf"

factor.data <- process.open(filename)
factor.data$date <- as.Date(as.character(factor.data$date), format="%Y%m%d")
factor.data$val <- factor.data$value
factor.data$value <- NULL

vars = c("val", "momentum", "size")
vars_2 = c("dividend_yield", "profitability", "growth")
vars_3 = c("earnings_variability", "trading_activity", "volatility", "leverage")

pdf(output)
data = melt(factor.data, measure.vars=vars)
ggplot(data, aes(date)) + geom_hline(yintercept=0)+
geom_line(aes(y=value))+
facet_grid(variable ~ .)

data = melt(factor.data, measure.vars=vars_2)
ggplot(data, aes(date)) + geom_hline(yintercept=0)+
geom_line(aes(y=value))+
facet_grid(variable ~ .)

data = melt(factor.data, measure.vars=vars_3)
ggplot(data, aes(date)) + geom_hline(yintercept=0)+
geom_line(aes(y=value))+
facet_grid(variable ~ .)

dev.off()

No comments:

Post a Comment