outlining (highlighting) pixels in ggplot2

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

outlining (highlighting) pixels in ggplot2

emorway
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]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: outlining (highlighting) pixels in ggplot2

Eik Vettorazzi-2
Hi Eric,
you can use an annotate-layer, eg

ind<-which(sig>0,arr.ind = T)
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)
+annotate("rect",ymin=ind[,"row"]-.5,ymax=.5+ind[,"row"],
xmin=-.5+ind[,"col"],xmax=.5+ind[,"col"],colour="red", size=.5,
linetype=1, fill=NA)


Cheers

Am 20.12.2017 um 01:32 schrieb Morway, Eric:

> 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)

--
Eik Vettorazzi

Department of Medical Biometry and Epidemiology
University Medical Center Hamburg-Eppendorf

Martinistrasse 52
building W 34
20246 Hamburg

Phone: +49 (0) 40 7410 - 58243
Fax:   +49 (0) 40 7410 - 57790
Web: www.uke.de/imbe
--

_____________________________________________________________________

Universitätsklinikum Hamburg-Eppendorf; Körperschaft des öffentlichen Rechts; Gerichtsstand: Hamburg | www.uke.de
Vorstandsmitglieder: Prof. Dr. Burkhard Göke (Vorsitzender), Prof. Dr. Dr. Uwe Koch-Gromus, Joachim Prölß, Martina Saurin (komm.)
_____________________________________________________________________

SAVE PAPER - THINK BEFORE PRINTING
______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: outlining (highlighting) pixels in ggplot2

emorway
I apprecaite the guidance Eik, that works great!  I'm also wondering if you
have any pointers for how I might stretch the color scale so that the max
and min values are the same?  Right now, the min is -0.064 and the max is
something closer to 0.04.  As you can see in what I sent, I tried adding:

zmax1 = max(abs(m1))

ggplot(..., autoscale = FALSE, zmin = -1 * zmax1, zmax = zmax1) + ...

to ggplot, but I'm either using the wrong arguments or have not added them
to the correct spot.  I haven't been able to find the fix for this and
would very much appreciate any further guidance you can offer for this last
fix.

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: outlining (highlighting) pixels in ggplot2

Eik Vettorazzi-2
just add

limits=c(-zmax1,zmax1)

to the scale_fill_gradient2-call

hth


Am 20.12.2017 um 18:21 schrieb Morway, Eric:

> I apprecaite the guidance Eik, that works great!  I'm also wondering if
> you have any pointers for how I might stretch the color scale so that
> the max and min values are the same?  Right now, the min is -0.064 and
> the max is something closer to 0.04.  As you can see in what I sent, I
> tried adding:
>
> zmax1 = max(abs(m1))
>
> ggplot(..., autoscale = FALSE, zmin = -1 * zmax1, zmax = zmax1) + ...
>
> to ggplot, but I'm either using the wrong arguments or have not added
> them to the correct spot.  I haven't been able to find the fix for this
> and would very much appreciate any further guidance you can offer for
> this last fix.  

--
Eik Vettorazzi

Universitätsklinikum Hamburg-Eppendorf
Institut für Medizinische Biometrie und Epidemiologie

Martinistraße 52
Gebäude W 34
20246 Hamburg

Telefon: +49 (0) 40 7410 - 58243
Fax:     +49 (0) 40 7410 - 57790

Web: www.uke.de/imbe


--

_____________________________________________________________________

Universitätsklinikum Hamburg-Eppendorf; Körperschaft des öffentlichen Rechts; Gerichtsstand: Hamburg | www.uke.de
Vorstandsmitglieder: Prof. Dr. Burkhard Göke (Vorsitzender), Prof. Dr. Dr. Uwe Koch-Gromus, Joachim Prölß, Martina Saurin (komm.)
_____________________________________________________________________

SAVE PAPER - THINK BEFORE PRINTING
______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.