简体   繁体   中英

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:

在此处输入图片说明

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?

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~.)

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM