모델의 매개 변수를 변경하는 분석이 있으며 다양한 설정 조합의 효과를 보여주는 잘 레이블이 지정된 그림을 생성하고 싶습니다. 문제없이 실제 플롯을 생성하고 그리드로 정렬 할 수 있습니다. 이제 데이터가 아니라 내가 변경하는 매개 변수 값을 참조하는 축을 추가하고 싶습니다. 다음은 내가하려는 간단한 예입니다.
library(lattice)
library(gridExtra)
dat <- rnorm(1000, 0, 1)
# these are my parameters
scales <- c(0.1, 0.5, 1, 10, 15)
adds <- c(1, 2, 3, 4, 5)
# generate data to be plotted
revData <- list()
ptr <- 1
for (ii in 1:length(scales)){
for (jj in 1:length(adds)){
revData[[ptr]] <- (dat * scales[[ii]]) + adds[[jj]]
ptr <- ptr + 1
}
}
# generating plots
plotList <- lapply(revData, function(xx){
xyplot(xx ~ 1:1000)
})
names(plotList) <- sprintf("N%s", 1:length(plotList))
plotList <- c(plotList, list(ncol=length(scales)))
do.call(grid.arrange, plotList)
그러면 다음 그림이 생성됩니다.
지금 제가하고 싶은 것은이 플롯 세트에 대해 X 축과 Y 축을 생성하는 것입니다. 여기서 X 축은 scales
변수를 참조하고 나열된 값을 가지고 있으며, 마찬가지로 값에 해당하는 Y 축도 adds
있습니다. 어떻게해야합니까?
Another answer using grid
package. First create a grid of viewports, then at each iteration of creating the data just plot it. This maybe gives a bit more control over spacing.
library(lattice)
library(grid)
# Data
dat <- rnorm(1000, 0, 1)
# these are my parameters
scales <- c(0.1, 0.5, 1, 10, 15)
adds <- c(1, 2, 3, 4, 5)
Create grid layout - this lets you look at the plot grid: 6 by 6 with the first column and last row used for the additional parameter labels.
grid.show.layout(grid.layout(6, 6,
widths = unit(c(2, rep(1,5)), c("lines", rep("null",5))) ,
heights = unit(c(rep(1,5),2), c(rep("null",5), "lines"))))
Start plot
grid.newpage()
pushViewport(viewport(layout=grid.layout(6, 6,
widths = unit(c(2, rep(1,5)), c("lines", rep("null",5))) ,
heights = unit(c(rep(1,5),2), c(rep("null",5), "lines")))))
# generate data and plot
for (ii in 1:length(scales)) {
for (jj in 1:length(adds)) {
revData <- (dat * scales[ii]) + adds[jj]
# plot (top to bottom / left to right)
pushViewport(viewport(layout.pos.row=jj, layout.pos.col=ii+1))
plot(xyplot(revData ~ 1:1000 , xlab=NULL , ylab=NULL) , newpage=FALSE)
upViewport(1)
}
}
add "scales" labels to x-axis
pushViewport(viewport(layout.pos.row=6, layout.pos.col=2:6 ))
grid.text(scales, x=seq(0.1,0.9 ,length=5), y=0.8 , hjust=0.2,
gp=gpar(col="red" , fontsize=14))
grid.text("Scales" , x=0.5 , y=0.3 , gp=gpar(fontsize=20))
grid.lines(y=1 , gp=gpar(col="red"))
upViewport(1)
add "adds" labels to y-axis
pushViewport(viewport(layout.pos.row=1:5, layout.pos.col=1 ))
grid.text(rev(adds),hjust=0.4 , x=0.8, y=seq(0.1,0.9 ,length=5), rot=90,
gp=gpar(col="red" , fontsize=14))
grid.text("Adds" , x=0.3 , y=0.5 , rot=90, gp=gpar(fontsize=20))
grid.lines(x=1 , gp=gpar(col="red"))
EDIT
I added axis titles using grid.text
and tweaking the x & y slightly. A better way would be to add another row and column to the grid and use this for the labels. An example grid is below - you will need to tweak another couple of things in the code but is not difficult.
grid.show.layout(grid.layout(7,7,
widths = unit(c(2, 2,rep(1,5)), c("lines", "lines",rep("null",5))) ,
heights = unit(c(rep(1,5),2,2), c(rep("null",5), "lines","lines"))))
축 제목에는 첫 번째 열과 마지막 행을, 축 레이블과 선에는 두 번째 열과 마지막 두 번째 행을 사용하여 나머지 그리드는 플롯에 사용할 수 있습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다