Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ importFrom(graphics,rect)
importFrom(graphics,segments)
importFrom(graphics,text)
importFrom(grid,addGrob)
importFrom(grid,convertHeight)
importFrom(grid,convertWidth)
importFrom(grid,gTree)
importFrom(grid,gpar)
importFrom(grid,grid.draw)
Expand Down
79 changes: 56 additions & 23 deletions R/methods-plotMatrix.R
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
#' @importFrom RColorBrewer brewer.pal
#' @importFrom plotgardener mapColors
#' @importFrom grid viewport unit grid.newpage gTree grid.ls rectGrob
#' addGrob grid.draw gpar
#' addGrob grid.draw gpar convertWidth convertHeight
#' @importFrom rlang inform
#' @importFrom grDevices colorRampPalette
#' @noRd
Expand Down Expand Up @@ -213,20 +213,31 @@

## If placing information is provided but plot == TRUE,
## set up it's own viewport separate from pageCreate

## Not translating into page_coordinates
if (is.null(matrixPlot$x) & is.null(matrixPlot$y)){

# Outer viewport
vp <- viewport(x=unit(0.5, "npc"),
y=unit(0.5, "npc"),
height=unit(1, "snpc"),
width=unit(1, "snpc"),
clip="on",
xscale=c(0, dims[2]),
yscale=c(dims[1], 0),
just="center",
name="MatrixPlot1")

name="MatrixPlot1_outside")
## Define inner viewport with appropriate matrix dimensions
# Get minimum scaling based on nrow and ncol
minScale <- min(1/dims)
# Multiply nrow and ncol ratios by minScale
vpDims <- dims*minScale

mat_vp <- viewport(x=unit(0.5, "npc"),
y=unit(0.5,"npc"),
height=unit(vpDims[1], "npc"),
width=unit(vpDims[2], "npc"),
xscale=c(0, dims[2]),
yscale=c(dims[1], 0),
just="center",
name="MatrixPlot1")

if (matrixPlot$draw){
grid.newpage()
}
Expand All @@ -246,17 +257,46 @@
## Convert coordinates into same units as page
page_coords <- plotgardener:::convert_page(matrixPlot)


## Make viewport
## Outer viewport
vp <- viewport(x=page_coords$x,
y=page_coords$y,
height=page_coords$height,
width=page_coords$width,
clip="on",
xscale=c(0, dims[2]),
yscale=c(dims[1], 0),
just=matrixPlot$just,
name=vp_name)
name=paste0(vp_name, "_outside"))

## Define inner viewport with appropriate matrix dimensions
# Convert nrow and ncol snpc scaling to page coordinates
mat_height <- convertHeight(unit(dims[1], "snpc"),
unitTo=get("page_units",
envir=plotgardener:::pgEnv),
valueOnly=TRUE)
mat_width <- convertWidth(unit(dims[2], "snpc"),
unitTo=get("page_units",
envir=plotgardener:::pgEnv),
valueOnly=TRUE)

# Get minimum scaling to outer viewport based on nrow and ncol
minScale <- min(as.numeric(page_coords$height)/mat_height,
as.numeric(page_coords$width)/mat_width)

# Multiply mat_height and mat_width by minScale
vpDims <- c(mat_height, mat_width)*minScale

mat_vp <- viewport(x=unit(0.5, "npc"),
y=unit(0.5,"npc"),
height=unit(vpDims[1],
get("page_units",
envir=plotgardener:::pgEnv)),
width=unit(vpDims[2],
get("page_units",
envir=plotgardener:::pgEnv)),
xscale=c(0, dims[2]),
yscale=c(dims[1], 0),
just="center",
name="MatrixPlot1")

}

## Handle graphical objects ----------------
Expand All @@ -272,24 +312,17 @@

## Assign name to grob
name <- paste0("MatrixPlot", nObjs + 1)

## Find the length of each square
maxDim <- which.max(dims)
if (maxDim == 1L) {
len <- vp$height/dims[maxDim]
} else {
len <- vp$width/dims[maxDim]
}

## Make grobs
## Make grobs within mat_vp
mpSquares <- rectGrob(
x=m_long$colnum,
y=m_long$rownum,
just=c("right", "top"),
width=len,
height=len,
width=1,
height=1,
gp=gpar(col=NA, fill=m_long$color),
default.units="native",
vp=mat_vp,
name=name
)

Expand Down