繁体   English   中英

大数据集和ggplot2中的构面(r)

[英]large dataset and faceting in ggplot2 (r)

几周前,当我尝试使用ggplot2中的构面( 为ggplot2(R)格式化构面的数据)时,@ CMichael帮了我 从他的解决方案开始,我现在需要使用略有不同的数据格式显示散点图,最终结果很奇怪。

让我告诉您我的代码(csv应该会自动下载,如果没有,请告诉我。不过警告:大约2 mb):

require(reshape2)
library(ggplot2)
library(RColorBrewer)

md = read.csv(file="http://dl.dropboxusercontent.com/u/73950/rob-136.csv", sep=",", header=TRUE)
dM = melt(md,c("id"))

#parse labels to identify "order" category and fill the value correspondingly
dM$order = ifelse(grepl("GED",dM$variable),"GED","NAR")

#parse labels to identify "Nm" category and fill the value correspondingly
dM$Nm = ifelse(grepl("FS",dM$variable),"FS",ifelse(grepl("VE",dM$variable),"VE",ifelse(grepl("N2",dM$variable),"N2","SW")))

#parse label to identify "category"
dM$category = ifelse(grepl("m", dM$variable),"m",
                     ifelse(grepl("mC",dM$variable),"mC",
                            ifelse(grepl("d",dM$variable),"d", 
                                   ifelse(grepl("nR",dM$variable),"nR","aSPL")))
                     )

# plot facet grid
p = ggplot(dM[dM$category=="d",],aes(x= dM[dM$category=="nR",]$value,y=dM[dM$category=="d",]$value))
p = p + scale_y_continuous(name="d")+ scale_x_continuous(name="nR") + xlim(0,136)
p = p + facet_grid(order~Nm)+ ggtitle("Title")
p = p + stat_bin2d(bins=50)
myPalette <- colorRampPalette(rev(brewer.pal(11, "Spectral")))
p = p + scale_fill_gradientn(colours = myPalette(100))
p = p + theme(legend.position="none")
p

在此处输入图片说明

我的问题如下:正确绘制了所有必需的散点图(无论如何我都认为它们是正确的),但是网格中图的位置都被弄乱了。

这是情节应该在的地方。 好像这里有图案... 在此处输入图片说明

问题1:代码中有明显的错误吗? 很可能我的数据操作或对构面的调用已完全关闭……有人可以为我解决问题吗?

问题2:除了该点之外,还可以...在x轴上显示“ nR”的正确方法是什么?

干杯!

如果您以与要绘制的变量相对应的方式排列数据,而不是尝试对数据进行子集化以提取所需的值,那么事情将会更加顺利。 下面是一个示例,并替换了您难以理解的嵌套ifelse部分。

dM = melt(md,c("id"))
# split variable out into its components
dM <- cbind(dM,
            colsplit(dM$variable,
                     pattern = "_",
                     names = c("Nm", "order", "category"))) 
# no longer need variable, as it is represented by the combination of Nm, order, and category
dM$variable <- NULL
# rearrange putting category in the columns
dM <- dcast(dM, ... ~ category, value.var = "value")

# plot
p = ggplot(dM, aes(x=nR ,y=d))

从那里您可以像以前一样继续。

暂无
暂无

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

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