简体   繁体   English

使用 ggplot2 在一个图上绘制多个图形

[英]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.我想用这个数据集: https ://dl.dropboxusercontent.com/u/73950/mydata.csv,以不同的色调显示 4 个不同的图形:GA、N1、N2、PE。 For each graphic, all values of category "m" must be displayed on the y axis, with "nbr" on the x axis.对于每个图形,所有类别“m”的值都必须显示在 y 轴上,“nbr”在 x 轴上。

Here's the code I have so far (thanks to @CMichael for most of this code)这是我到目前为止的代码(感谢@CMichael 提供了大部分代码)

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.一方面,我似乎无法在 x 轴上强制使用“nbr”。 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:因此,根据 OP 的评论,这可能是绘制数据的一种方法:

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:根据 OP 的评论,这可能是绘制数据的一种方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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