Lucien S.
Lucien S.

Reputation: 5345

formatting data for faceting in ggplot2 (R)

I'm trying to accomplish something in ggplot2 that shouldn't be a big deal, but that blocks me somehow.

I need to import this xls file: https://dl.dropboxusercontent.com/u/73950/mydata.xls and format it so I can display 4 facetted line graphs (titled "m", "mNC", "d", "aSPL") like in this model drawed by myself (only showing the "mNC" facet, but the other one should be built on the same model):

this is what I would like to get...

Now, the trick, I think, is that the columns are named as such: "PE-GED-nMC", "GA-GED-nMC", "N1-GED-nMC", "N2-GED-nMC", etc. and I need to somehow tell R to arrange data according to parts of these column names. I think...

Does anyone have a clue about how to get from my data.xls to 4 faceted figures?

Cheers!

Upvotes: 1

Views: 527

Answers (1)

CMichael
CMichael

Reputation: 1866

I think this should deliver what you are looking for - applying melt as suggested by Matt. The grepl-function should help you parse the text labels. I did not know what the horizontal and the vertical categories really are so just gave them generic names.

Clearly the ifelse-constructs are somewhat cumbersome and may warrant a more elegant solution in a more complex setting.

require(reshape2)
#use melt to go from wide to long data
dataM = melt(data,c("nbr"))

#parse labels to identify vertical category and fill the value correspondingly
dataM$vertical = ifelse(grepl("GED",dataM$variable),"GED",ifelse(grepl("RAN",dataM$variable),"RAN",ifelse(grepl("EIG",dataM$variable),"EIG","BET")))
#parse labels to identify horizontal category and fill the value correspondingly
dataM$horizontal = ifelse(grepl("PE",dataM$variable),"PE",ifelse(grepl("GA",dataM$variable),"GA",ifelse(grepl("N1",dataM$variable),"N1","N2")))
#parse label to identify category
dataM$category = ifelse(grepl("mNC",dataM$variable),"mNC",ifelse(grepl("aSPL",dataM$variable),"aSPL",ifelse(grepl("_d",dataM$variable),"d","m")))

#create ggplot objects with sub-setted data
p1 = ggplot(dataM[dataM$category=="mNC",],aes(x=nbr,y=value))
p1 = p1 + geom_line()
#face_grid creates the panels that you are looking for (usage is vertical_categories ~ horizontal_categories)
p1 = p1 + facet_grid(vertical~horizontal)
p1

p2 = ggplot(dataM[dataM$category=="aSPL",],aes(x=nbr,y=value))
p2 = p2 + geom_line()
p2 = p2 + facet_grid(vertical~horizontal)
p2

p3 = ggplot(dataM[dataM$category=="d",],aes(x=nbr,y=value))
p3 = p3 + geom_line()
p3 = p3 + facet_grid(vertical~horizontal)
p3

p4 = ggplot(dataM[dataM$category=="m",],aes(x=nbr,y=value))
p4 = p4 + geom_line()
p4 = p4 + facet_grid(vertical~horizontal)
p4

Upvotes: 2

Related Questions