使用ggplot()和facet_grid()绘制多个图形
我想知道如何使用使用ggplot()和facet_grid()绘制多个图形,r,ggplot2,ecdf,facet-grid,R,Ggplot2,Ecdf,Facet Grid,我想知道如何使用ggplot()和facet\u grid()在一个屏幕上绘制多个图形,因为我确实需要对不同的统计变量重复此过程数次 我有两个数据框,一个包含观察结果,另一个包含预测。它们都是550 x 76的矩阵 数据帧1: Observations x1 x2 x3 x5 x6 x7 x8 x9 x10 x11 x13 .... x75 Observation1 -0.1 0.05 0.1 0.2 0.3 0.04 0.3 -0.1 0.02 0.02 -0
ggplot()
和facet\u grid()
在一个屏幕上绘制多个图形,因为我确实需要对不同的统计变量重复此过程数次
我有两个数据框,一个包含观察结果,另一个包含预测。它们都是550 x 76
的矩阵
数据帧1:
Observations x1 x2 x3 x5 x6 x7 x8 x9 x10 x11 x13 .... x75
Observation1 -0.1 0.05 0.1 0.2 0.3 0.04 0.3 -0.1 0.02 0.02 -0.2 ....0.8
Observation2 -0.3 0.05 0.1 0.1 0.3 0.03 0.3 -0.1 0.03 0.02 -0.2 ....0.6
Observation3 -0.2 0.05 0.1 0.4 0.3 0.02 0.3 -0.1 0.01 0.01 -0.2 ....0.1
Observation550 -0.1 0.05 0.8 0.4 0.3 0.02 0.7 -0.1 0.01 0.01 -0.2 ....0.1
数据帧2:
Predictions x1 x2 x3 x5 x6 x7 x8 x9 x10 x11 x13 .... x75
Prediction1 -0.1 0.05 0.1 0.2 0.3 0.04 0.3 -0.1 0.02 0.02 -0.2 ....0.8
Prediction2 -0.3 0.05 0.1 0.1 0.3 0.03 0.3 -0.1 0.03 0.02 -0.2 ....0.6
Prediction3 -0.2 0.05 0.1 0.4 0.3 0.02 0.3 -0.1 0.01 0.01 -0.2 ....0.1
Prediction550 -0.1 0.05 0.8 0.4 0.3 0.02 0.7 -0.1 0.01 0.01 -0.2 ....0.1
为了使用ggplot()
,我似乎只需要创建一个包含这两个矩阵的数据帧或列表
我已经这样做了,但是使用了r
的标准绘图
提前谢谢我想这就是你想要的
library(ggplot2)
library(reshape2)
# Fake some data
set.seed(1234)
nc <- 15
nr <- 20
onms <- sprintf("Observation%d",1:nr)
pnms <- sprintf("Prediction%d",1:nr)
cnames <- sprintf("x%d",1:nc)
odf <- data.frame(Observations=onms)
pdf <- data.frame(Predictions=pnms)
for(i in 1:nc){
vk1 <- 0.01*rnorm(nr)
odf[[cnames[i]]] <- round(cumsum(vk1),3)
vk2 <- 0.02*rnorm(nr)
pdf[[cnames[i]]] <- round(cumsum(vk1) + cumsum(vk2),3)
}
# This is the data we need
head(odf)
head(pdf)
# Now change the pred. colnames so they don't collide with the obs. colnames
newpnames <- sprintf("p_x%d",1:nc)
names(pdf) <- c("series",newpnames)
names(odf)[1] <- "series"
# Merge the data into a long format
modf <- melt(odf,id.vars="series",measure.vars=cnames)
mpdf <- melt(pdf,id.vars="series",measure.vars=newpnames)
mdf <- rbind(modf,mpdf)
# Now extract the fields we need into new columns
mdf$x <- as.numeric(gsub(".*[A-Za-z]","",mdf$variable))
mdf$frame <- as.numeric(gsub(".*[A-Za-z]","",mdf$series))
frameNames <- sprintf("Frame:%d",1:max(mdf$frame))
mdf$frame <- factor(sprintf("Frame:%d",mdf$frame),levels=frameNames)
mdf$kind <- substr(mdf$series,1,3)
# Finally plot it
ggplot(mdf) + geom_line(aes(x=x,y=value,color=kind)) + facet_wrap( ~ frame )
# ecdf version
ggplot(mdf,aes(x=value,color=kind)) + stat_ecdf(geom="step") + facet_wrap( ~ frame )
最后得出这个图:
这是分面的ecdf
图:
您希望镶嵌面的外观如何?一个76列550行的网格将超过我所见过的任何输出设备。我想显示至少两个屏幕和选定的行(每个图形是一行75列),可以是两个屏幕,比如说每个20个图形。好的,是的,我看到我误解了它。给我几分钟这个答案有什么问题吗?现在我想起来了,我应该按行生成假数据,并用
rbind
而不是按列将其添加到数据帧中。这就是为什么它看起来如此奇怪的图案,但这对你来说并不重要。嘿,谢谢@Mike wise,你的回答真的很有用。唯一需要考虑的是,我的图形以不同的顺序显示,用这个命令:mdf Out shopping now将向后看,因此我添加了一个更稳定的框架标题,以及ecdf
绘图。高兴吗?如能投票,将不胜感激:)
> head(odf)
Observations x1 x2 x3 x4 x5 x6 x7 x8 x9
1 Observation1 -0.012 0.014 -0.002 -0.002 -0.008 0.005 0.001 -0.010 0.001
2 Observation2 -0.009 0.004 -0.003 -0.010 -0.011 0.012 0.005 -0.005 0.002
3 Observation3 0.002 -0.005 -0.017 0.011 -0.015 0.014 -0.006 -0.012 -0.003
4 Observation4 -0.022 -0.008 -0.019 0.018 -0.017 0.021 0.001 -0.004 -0.019
5 Observation5 -0.018 -0.017 -0.010 0.037 -0.013 0.024 0.008 -0.012 -0.019
6 Observation6 -0.013 -0.027 -0.003 0.037 -0.007 0.031 0.011 -0.009 -0.026
x10 x11 x12 x13 x14 x15
1 0.009 -0.012 0.005 -0.007 0.015 -0.007
2 0.028 -0.012 0.004 0.005 0.013 -0.018
3 0.028 -0.016 0.005 -0.012 0.026 -0.021
4 0.027 -0.025 -0.004 -0.008 0.026 -0.020
5 0.022 -0.021 -0.017 -0.006 0.019 -0.012
6 0.036 -0.019 -0.003 0.026 0.011 0.001
> head(pdf)
Predictions x1 x2 x3 x4 x5 x6 x7 x8 x9
1 Prediction1 -0.009 0.028 0.007 -0.009 -0.063 0.023 0.020 -0.022 0.000
2 Prediction2 -0.016 0.068 -0.005 0.011 -0.068 0.043 0.017 -0.036 0.007
3 Prediction3 -0.014 0.059 -0.017 0.045 -0.052 0.090 0.009 -0.047 0.021
4 Prediction4 -0.029 0.042 -0.029 0.050 -0.046 0.121 -0.019 -0.018 0.028
5 Prediction5 -0.038 0.032 -0.037 0.079 -0.024 0.130 -0.005 -0.026 0.031
6 Prediction6 -0.062 0.058 -0.027 0.087 0.022 0.124 -0.016 -0.036 0.047
x10 x11 x12 x13 x14 x15
1 -0.027 -0.037 0.007 0.012 0.023 -0.026
2 -0.061 -0.029 -0.010 0.000 0.048 -0.027
3 -0.073 -0.035 -0.004 -0.003 0.048 -0.023
4 -0.045 -0.041 0.000 -0.001 0.048 -0.025
5 -0.034 -0.024 -0.038 0.037 0.030 -0.007
6 0.005 -0.020 -0.045 0.064 -0.002 -0.005