Skip to content

Commit

Permalink
tested circos.raster()
Browse files Browse the repository at this point in the history
  • Loading branch information
jokergoo committed Jun 15, 2017
1 parent 558c0a9 commit eb0359f
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 23 deletions.
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
Package: circlize
Type: Package
Title: Circular Visualization
Version: 0.4.0
Version: 0.4.1
Date: 2017-5-29
Author: Zuguang Gu
Maintainer: Zuguang Gu <[email protected]>
Depends: R (>= 3.0.0), graphics
Imports: GlobalOptions (>= 0.0.12), shape, grDevices, utils, stats,
colorspace, methods, grid
Suggests: knitr, dendextend (>= 1.0.1), ComplexHeatmap (>= 1.13.2), gridBase
Suggests: knitr, dendextend (>= 1.0.1), ComplexHeatmap (>= 1.13.2), gridBase,
png
VignetteBuilder: knitr
Description: Circular layout is an efficient way for the visualization of huge
amounts of information. Here this package provides an implementation
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ importFrom("colorspace", coords)
importFrom("colorspace", hex)
importFrom("colorspace", sRGB)
importFrom("grDevices", "dev.off", "pdf")
importFrom("grDevices", as.raster)
importFrom("grDevices", col2rgb)
importFrom("grDevices", rgb)
importFrom("grid", grid.pretty)
Expand Down
6 changes: 6 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
Changes in version 0.4.1
---------------------------------------------------------------------------

* add `circos.raster()`


Changes in version 0.4.0
----------------------------------------------------------------------------

Expand Down
51 changes: 32 additions & 19 deletions R/raster.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,60 @@
# Add raster images
#
# == param
# -image a ``raster`` object, or an object that can be converted by `graphics::as.raster`
# -image a ``raster`` object, or an object that can be converted by `grDevices::as.raster`
# -x position of the center of the raster image, measued in the data coordinate in the cell
# -y position of the center of the raster image, measued in the data coordinate in the cell
# -width width of the raster image. When ``facing`` is one of "inside", "outside", "clockwise"
# and "reverse.clockwise", the image should have absolute size where the value of ``width``
# should be specified as ``20mm``, ``1cm`` or ``0.5inche``. When ``facing`` is one of
# should be specified like ``20mm``, ``1cm`` or ``0.5inche``. When ``facing`` is one of
# ``bending.inside`` and ``bending.outside``, the value of ``width`` is measured in the data
# coordinate in the cell.
# -height height of the raster image. Same format as ``width``. If the value of ``height`` is omit,
# default height is calculated by taking the aspect ratio of the original image.
# default height is calculated by taking the aspect ratio of the original image. But when
# ``facing`` is one of ``bending.inside`` and ``bending.outside``, ``height`` is mandatory to set.
# -facing facing of the raster image
# -niceFacing facing of text. Please refer to vignette for different settings
# -sector.index index for the sector
# -track.index index for the track
# -scaling scaling factor to resize the raster image. The size measured in pixel is calculated according
# to the size in the final plot. The size can be adjusted by multiplying ``scaling``.
# -scaling scaling factor to resize the raster image.
#
# == author
# Zuguang Gu <[email protected]>
#
# == example
# require(png)
# image = system.file("img", "Rlogo.png", package = "png")
# image = system.file("extdata", "Rlogo.png", package = "circlize")
# image = as.raster(readPNG(image))
# circos.initialize(letters[1:8], xlim = c(0, 1))
# circos.track(ylim = c(0, 1), panel.fun = function(x, y) {
# circos.raster(image, CELL_META$xcenter, CELL_META$ycenter, width = "2cm",
# facing = "inside", niceFacing = TRUE)
# circos.raster(image, CELL_META$xcenter, CELL_META$ycenter, width = "2cm",
# facing = "inside", niceFacing = TRUE)
# })
#
# circos.clear()

# \dontrun{
# circos.initialize(letters[1:8], xlim = c(0, 1))
# # NOTE: following takes quite a long time to run
# load(system.file("extdata", "doodle.RData", package = "circlize"))
# circos.par("cell.padding" = c(0, 0, 0, 0))
# circos.initialize(letters[1:16], xlim = c(0, 1))
# circos.track(ylim = c(0, 1), panel.fun = function(x, y) {
# circos.raster(image, CELL_META$xcenter, CELL_META$ycenter, width = 1,
# height = 1, facing = "bending.inside")
# })
# img = img_list[[CELL_META$sector.numeric.index]]
# circos.raster(img, CELL_META$xcenter, CELL_META$ycenter, width = 1,
# height = 1, facing = "bending.inside")
# }, track.height = 0.25, bg.border = NA)
# circos.track(ylim = c(0, 1), panel.fun = function(x, y) {
# img = img_list[[CELL_META$sector.numeric.index + 16]]
# circos.raster(img, CELL_META$xcenter, CELL_META$ycenter, width = 1,
# height = 1, facing = "bending.inside")
# }, track.height = 0.25, bg.border = NA)
# circos.clear()
# }
circos.raster = function(image, x, y, width, height,
facing = c("inside", "outside", "reverse.clockwise", "clockwise",
"downward", "bending.inside", "bending.outside"),
niceFacing = FALSE, sector.index = get.cell.meta.data("sector.index"),
track.index = get.cell.meta.data("track.index"),
scaling = ifelse(grepl("bend", facing), 0.5, 1)) {
scaling = 1) {

# convert image to raster class
if(!inherits(image, "raster")) {
Expand Down Expand Up @@ -81,13 +92,13 @@ circos.raster = function(image, x, y, width, height,
height_in_native = height/(cell.ylim[2] - cell.ylim[1]) * (yplot[2] - yplot[1])

r = yplot[1] + (y - cell.ylim[1])/(cell.ylim[2] - cell.ylim[1]) * (yplot[2] - yplot[1])
width_in_native = pi*as.radian(width_in_degree)*r
width_in_native = as.radian(width_in_degree)*r

width_in_px = round(width_in_native/pt_per_inche1*96)
height_in_px = round(height_in_native/pt_per_inche1*96)

# resize according to the size on the image
image = resize_image(image, w = width_in_px*scaling, h = height_in_px*scaling)
image = resize_image(image, w = ncol(image)*scaling, h = nrow(image)*scaling)

# make circular rectangles
xlim = get.cell.meta.data("xlim", sector.index = sector.index, track.index = track.index)
Expand All @@ -108,10 +119,12 @@ circos.raster = function(image, x, y, width, height,
}
xv = x - width/2 + xv*width
yv = y - height/2 + yv*height
l = !grepl("^#FFFFFF", image)
l = !grepl("^#FFFFFF00", image)
op = circos.par("points.overflow.warning")
circos.par(points.overflow.warning = FALSE)
circos.rect(xv[l] - width/nc/2, yv[l] - height/nr/2, xv[l] + width/nc/2, yv[l] + height/nr/2,
col = image[l], border = image[l], sector.index = sector.index, track.index = track.index)

circos.par(points.overflow.warning = op)
} else {
if(missing(width) && missing(height)) {
stop("at least one of `width` and `height` should be specified")
Expand Down Expand Up @@ -156,7 +169,7 @@ circos.raster = function(image, x, y, width, height,
y0 = df2[1, 2]

# resize accoring to the width and height
image = resize_image(image, w = width_in_px*scaling, h = height_in_px*scaling)
image = resize_image(image, w = ncol(image)*scaling, h = nrow(image)*scaling)

coor_mat = cbind(c(x0 - width_in_native/2, y0 - height_in_native/2),
c(x0 - width_in_native/2, y0 + height_in_native/2),
Expand Down
Binary file added inst/extdata/Rlogo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions man/circos.raster.rd
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ circos.raster(image, x, y, width, height,
}
\arguments{

\item{image}{a \code{raster} object, or an object that can be converted by \code{\link[graphics]{as.raster}}}
\item{image}{a \code{raster} object, or an object that can be converted by \code{\link[grDevices]{as.raster}}}
\item{x}{position of the center of the raster image, measued in the data coordinate in the cell}
\item{y}{position of the center of the raster image, measued in the data coordinate in the cell}
\item{width}{width of the raster image. When \code{facing} is one of "inside", "outside", "clockwise" and "reverse.clockwise", the image should have absolute size where the value of \code{width} should be specified as \code{20mm}, \code{1cm} or \code{0.5inche}. When \code{facing} is one of \code{bending.inside} and \code{bending.outside}, the value of \code{width} is measured in the data coordinate in the cell.}
Expand All @@ -33,7 +33,7 @@ Zuguang Gu <[email protected]>
}
\examples{
require(png)
image = system.file("img", "Rlogo.png", package = "png")
image = system.file("extdata", "Rlogo.png", package = "circlize")
image = as.raster(readPNG(image))
circos.initialize(letters[1:8], xlim = c(0, 1))
circos.track(ylim = c(0, 1), panel.fun = function(x, y) {
Expand Down

0 comments on commit eb0359f

Please sign in to comment.