我试图在ggplot2中完成一些不应该成为大问题的东西,但这会阻止我。
我需要导入这个xls文件:https://dl.dropboxusercontent.com/u/73950/mydata.xls并对其进行格式化以便显示4个刻面线图(标题为&#34; m&#34;,&#34; mNC&#34;,&#34 ; d&#34;,&#34; aSPL&#34;)就像在我自己绘制的这个模型中一样(仅显示&#34; mNC&#34; facet,但另一个应该建立在同一模型上):< / p>
现在,我认为,诀窍是列的名称如下:&#34; PE-GED-nMC&#34;,&#34; GA-GED-nMC&#34;,&#34; N1-GED-nMC&#34;,&#34; N2-GED-nMC&#34;等我需要以某种方式告诉R根据这些列名的部分来排列数据。我想......
有没有人知道如何从我的data.xls到4个多面的数字?
干杯!
答案 0 :(得分:2)
我认为这应该能满足您的需求 - 按照Matt的建议应用熔体。 grepl-function应该可以帮助您解析文本标签。我不知道水平和垂直类别究竟是什么,所以只给它们通用名称。
显然,ifelse构造有点麻烦,并且可以在更复杂的环境中保证更优雅的解决方案。
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