Lucien S.
Lucien S.

Reputation: 5345

several graphs on one plot with ggplot2

I would like, with this data set: https://dl.dropboxusercontent.com/u/73950/mydata.csv , to display 4 different graphics: GA, N1, N2, PE in different shades. For each graphic, all values of category "m" must be displayed on the y axis, with "nbr" on the x axis.

Here's the code I have so far (thanks to @CMichael for most of this code)

require(reshape2)

mydata = read.csv(file="/Users/Rodolphe/Downloads/mydata.csv", sep=";", header=TRUE)
dataM = melt(mydata,c("nbr"))

#parse labels to identify vertical category and fill the value correspondingly
dataM$order = 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$net = 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")))


ggplot(dataM[dataM$category=="m" & dataM$order=="RAN",], aes(value, fill=net)) + geom_density(alpha = .3, color=NA)  + scale_fill_brewer(palette="Set1")

Which gives me:

enter image description here

That's, aesthetically, exactly what I need. The display is obviously wrong, however, and several things confuse me. For one thing, I can't seem to force "nbr" on the x axis. Am I on the right track at all with this code?

Upvotes: 0

Views: 431

Answers (2)

Azam
Azam

Reputation: 1

based on OP's comments, this might be one way to plot the data:

ggplot(dataM[dataM$category=="m" & dataM$order=="RAN",], aes(x=nbr, y=value, fill=net)) + 
  geom_ribbon(aes(ymin=0, ymax=value),alpha=0.3)+ 
  scale_fill_brewer(palette="Set1")

Upvotes: 0

jlhoward
jlhoward

Reputation: 59335

So based on OP's comments, this might be one way to plot the data:

ggplot(dataM[dataM$category=="m" & dataM$order=="RAN",], aes(x=nbr, y=value, fill=net)) + 
  geom_ribbon(aes(ymin=0, ymax=value),alpha=0.3)+ 
  scale_fill_brewer(palette="Set1")

Or, IMHO a better option:

ggplot(dataM[dataM$category=="m" & dataM$order=="RAN",], aes(x=nbr, y=value, fill=net)) + 
  geom_line(aes(color=net))+
  geom_ribbon(aes(ymin=0, ymax=value),alpha=0.3)+ 
  scale_fill_brewer(palette="Set1")+
  facet_grid(net~.)

Upvotes: 1

Related Questions