[R] outlining (highlighting) pixels in ggplot2

Morway, Eric emorway at usgs.gov
Wed Dec 20 01:32:23 CET 2017


Using the small reproducible example below, I'd like to know if one can
somehow use the matrix "sig" (defined below) to add a black outline (with
lwd=2) to all pixels with a corresponding value of 1 in the matrix 'sig'?
So for example, in the ggplot2 plot below, the pixel located at [1,3] would
be outlined by a black square since the value at sig[1,3] == 1.  This is my
first foray into ggplot2, and so far the googles hasn't helped me determine
if this is possible.  Thanks, Eric

PS, my attempt to stretch the color scale such that (-1 * zmin) = zmax =
max(abs(m1)) has failed (i.e., ..., autoscale = FALSE, zmin = -1 * zmax1,
zmax = zmax1), any pointers on this would greatly appreciated as well.

# Import packages
library(ggplot2)
library(RColorBrewer)
library(reshape2)

m1 <- matrix(c(
-0.0024, -0.0031, -0.0021, -0.0034, -0.0060, -1.00e-02, -8.47e-03, -0.0117,
-0.0075, -0.0043, -0.0026, -0.0021,
-0.0015, -0.0076, -0.0032, -0.0105, -0.0107, -2.73e-02, -3.37e-02, -0.0282,
-0.0149, -0.0070, -0.0046, -0.0039,
-0.0121, -0.0155, -0.0203, -0.0290, -0.0330, -3.19e-02, -1.74e-02, -0.0103,
-0.0084, -0.0180, -0.0162, -0.0136,
-0.0073, -0.0053, -0.0050, -0.0058, -0.0060, -4.38e-03, -2.21e-03, -0.0012,
-0.0026, -0.0026, -0.0034, -0.0073,
-0.0027, -0.0031, -0.0054, -0.0069, -0.0071, -6.28e-03, -2.88e-03, -0.0014,
-0.0031, -0.0037, -0.0030, -0.0027,
-0.0261, -0.0223, -0.0216, -0.0293, -0.0327, -3.17e-02, -1.77e-02, -0.0084,
-0.0059, -0.0060, -0.0120, -0.0157,
 0.0045,  0.0006, -0.0031, -0.0058, -0.0093, -9.20e-03, -6.76e-03,
-0.0033,  0.0002,  0.0045,  0.0080,  0.0084,
-0.0021, -0.0018, -0.0020, -0.0046, -0.0080, -2.73e-03,  7.43e-04,  0.0004,
-0.0010, -0.0017, -0.0022, -0.0024,
-0.0345, -0.0294, -0.0212, -0.0194, -0.0192, -2.25e-02, -2.05e-02, -0.0163,
-0.0179, -0.0213, -0.0275, -0.0304,
-0.0034, -0.0038, -0.0040, -0.0045, -0.0059, -1.89e-03,  6.99e-05, -0.0050,
-0.0114, -0.0112, -0.0087, -0.0064,
-0.0051, -0.0061, -0.0052, -0.0035,  0.0012, -7.41e-06, -3.43e-03, -0.0055,
-0.0020,  0.0016, -0.0024, -0.0069,
-0.0061, -0.0068, -0.0089, -0.0107, -0.0104, -7.65e-03,  2.43e-03,  0.0008,
-0.0006, -0.0014, -0.0021, -0.0057,
 0.0381,  0.0149, -0.0074, -0.0302, -0.0550, -6.40e-02, -5.28e-02, -0.0326,
-0.0114,  0.0121,  0.0367,  0.0501,
-0.0075, -0.0096, -0.0123, -0.0200, -0.0288, -2.65e-02, -2.08e-02, -0.0176,
-0.0146, -0.0067, -0.0038, -0.0029,
-0.0154, -0.0162, -0.0252, -0.0299, -0.0350, -3.40e-02, -2.51e-02, -0.0172,
-0.0139, -0.0091, -0.0119, -0.0156),
  nrow = 15, ncol = 12, byrow=TRUE,
  dimnames = list(rev(c("TH1", "IN1", "IN3", "GL1", "LH1", "ED9", "TC1",
"TC2", "TC3", "UT1", "UT3", "UT5", "GC1", "BC1", "WC1")),
                              c(format(seq(as.Date('2000-10-01'),
as.Date('2001-09-30'), by='month'), "%b"))))

# palette definition
palette <- colorRampPalette(c("darkblue", "blue", "white", "red",
"darkred"))

# find max stretch value
zmax1 = max(abs(m1))

m1.melted <- melt(m1)
names(m1.melted) <- c('Site','Month', 'Concentration')

# Set up an example matrix with binary code for which results (pixels) are
significant
set.seed(4004)
sig <- matrix(round(abs(rnorm(15*12)/3)), nrow = 15, ncol = 12)

ggplot(m1.melted, aes(x = Month, y = Site, fill = Concentration), autoscale
= FALSE, zmin = -1 * zmax1, zmax = zmax1) +
  geom_tile() +
  coord_equal() +
  scale_fill_gradient2(low = "darkred",
                       mid = "white",
                       high = "darkblue",
                       midpoint = 0)

	[[alternative HTML version deleted]]



More information about the R-help mailing list