1

I have data d like this:

    hod        dow     gid.most     gid.perc        gid.n
100   1 2013-08-09 6421.0000000    1.0000000   15.0000000
101   2 2013-08-09 6421.0000000    1.0000000    7.0000000
102   7 2013-08-09 6458.0000000    1.0000000    1.0000000
103   8 2013-08-09 6458.0000000    1.0000000    6.0000000
104   9 2013-08-09 6458.0000000    0.8181818    9.0000000
105  10 2013-08-09 6458.0000000    1.0000000   11.0000000
106  11 2013-08-09 6458.0000000    0.9545455   21.0000000
107  12 2013-08-09 6458.0000000    0.6000000   12.0000000

It is basically a data.frame with a matrix gid. hod is the hour of the day, dow is the day of the week in Date class (forget August, that's just how it was transformed). gid.most is a discrete value, gid.perc gives the percentage of how this discrete value appeared in relation to other values (it does not matter how this is calculated), and gid.n gives how many times this certain, discrete value occured.

I want to produce a carpet plot to visually recognize certain temporal patterns in values. That's what I have:

p <- ggplot(d,aes(dow,hod)) +

  geom_tile(aes(fill = as.factor(gid[,'most']), alpha = gid[,'perc'])) +
  geom_point() +
  scale_fill_discrete() + 
  ylab("Hour of day") + xlab("Day of week") +
  # hour scale
  scale_y_continuous(limits=c(-0.5,23.5),
                     breaks=c(0:8*3 - 0.5),
                     labels=ifelse(
                       c(0:8*3) < 10,
                       paste('0',c(0:8*3),':00',sep=''),
                       paste(c(0:8*3),':00',sep='')
                     )
  ) +
  scale_y_reverse() +
  # week scale
  scale_x_date(labels = date_format('%a')) +
  theme_minimal() + 
  theme(plot.margin = unit(c(0.5,0.2,0.5,0.5), "cm"), 
        axis.title.x = element_text(vjust=-1),
        axis.title.y = element_text(angle=90, vjust=0),
        legend.position = "none",
        panel.grid.major = element_line(colour=rgb(0.87,0.87,0.87)),
        panel.grid.minor = element_blank(),
        plot.background = element_rect(fill = rgb(0.97,0.97,0.97), linetype=0)
  )
p

And that's what it gives:

enter image description here

In this case, there are only two different discrete values, represented by red and blue. The tiles' alpha value represents the percentage such a value occurs (I could also mix two colors together, but that would be too complicated).

I added geom_point in order to also display gid.n. I thought about setting the alpha of each point, so that one could recognize how many times the value occurs and visually grasp the "significance" of each tile by looking at the points only.

So I only changed:

geom_point(aes(alpha = gid[,'n']))

This seems to correctly set the alpha of each point according to gid.n, but it also seems that all the tiles get the same, low alpha, not based on gid.perc.

enter image description here

How can I "grapically separate" points from tiles?

Here you get the whole data for the above example, in case you want to reproduce:

structure(list(hod = c(0L, 1L, 2L, 3L, 4L, 10L, 11L, 12L, 13L, 
14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 0L, 1L, 2L, 
3L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 15L, 16L, 17L, 18L, 19L, 
20L, 21L, 22L, 23L, 0L, 1L, 2L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 
14L, 15L, 16L, 17L, 19L, 20L, 21L, 22L, 23L, 0L, 1L, 2L, 7L, 
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 
22L, 23L, 0L, 1L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 
16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 0L, 1L, 2L, 7L, 8L, 9L, 
10L, 11L, 12L, 13L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 
0L, 1L, 2L, 3L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 
19L, 20L, 21L, 22L, 23L), dow = structure(c(15921, 15921, 15921, 
15921, 15921, 15921, 15921, 15921, 15921, 15921, 15921, 15921, 
15921, 15921, 15921, 15921, 15921, 15921, 15921, 15922, 15922, 
15922, 15922, 15922, 15922, 15922, 15922, 15922, 15922, 15922, 
15922, 15922, 15922, 15922, 15922, 15922, 15922, 15922, 15922, 
15922, 15923, 15923, 15923, 15923, 15923, 15923, 15923, 15923, 
15923, 15923, 15923, 15923, 15923, 15923, 15923, 15923, 15923, 
15923, 15923, 15924, 15924, 15924, 15924, 15924, 15924, 15924, 
15924, 15924, 15924, 15924, 15924, 15924, 15924, 15924, 15924, 
15924, 15924, 15924, 15925, 15925, 15925, 15925, 15925, 15925, 
15925, 15925, 15925, 15925, 15925, 15925, 15925, 15925, 15925, 
15925, 15925, 15925, 15925, 15925, 15926, 15926, 15926, 15926, 
15926, 15926, 15926, 15926, 15926, 15926, 15926, 15926, 15926, 
15926, 15926, 15926, 15926, 15926, 15926, 15920, 15920, 15920, 
15920, 15920, 15920, 15920, 15920, 15920, 15920, 15920, 15920, 
15920, 15920, 15920, 15920, 15920, 15920, 15920), class = "Date"), 
    gid = structure(c(6421, 6421, 6421, 6421, 6421, 6421, 6421, 
    6421, 6421, 6421, 6421, 6421, 6421, 6421, 6421, 6421, 6421, 
    6421, 6421, 6421, 6421, 6421, 6421, 6421, 6458, 6458, 6421, 
    6421, 6458, 6458, 6421, 6421, 6421, 6421, 6421, 6458, 6421, 
    6421, 6421, 6421, 6421, 6421, 6421, 6458, 6458, 6458, 6458, 
    6458, 6421, 6458, 6458, 6421, 6421, 6458, 6421, 6421, 6421, 
    6421, 6421, 6421, 6421, 6421, 6421, 6458, 6458, 6421, 6458, 
    6458, 6421, 6458, 6458, 6458, 6421, 6421, 6421, 6421, 6421, 
    6421, 6421, 6421, 6421, 6421, 6458, 6458, 6458, 6458, 6458, 
    6421, 6421, 6421, 6458, 6421, 6421, 6421, 6421, 6421, 6421, 
    6421, 6421, 6421, 6421, 6458, 6458, 6458, 6458, 6458, 6458, 
    6421, 6421, 6421, 6421, 6458, 6421, 6421, 6421, 6421, 6421, 
    6421, 6421, 6421, 6421, 6421, 6421, 6421, 6421, 6421, 6421, 
    6421, 6421, 6421, 6421, 6421, 6421, 6421, 6421, 6421, 1, 
    0.870967741935484, 1, 0.708333333333333, 1, 0.666666666666667, 
    0.611111111111111, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 0.666666666666667, 1, 0.857142857142857, 0.727272727272727, 
    0.5, 0.777777777777778, 1, 0.857142857142857, 1, 0.75, 1, 
    0.5, 0.703703703703704, 0.833333333333333, 1, 1, 1, 1, 1, 
    0.6, 0.7, 0.666666666666667, 0.714285714285714, 1, 0.538461538461538, 
    0.75, 0.583333333333333, 0.6, 0.722222222222222, 1, 0.842105263157895, 
    0.878787878787879, 1, 1, 1, 1, 1, 1, 1, 0.75, 0.5, 0.5625, 
    0.5, 0.666666666666667, 0.571428571428571, 0.857142857142857, 
    0.692307692307692, 0.714285714285714, 1, 0.545454545454545, 
    0.909090909090909, 0.90625, 1, 1, 1, 1, 1, 1, 0.75, 1, 0.8, 
    0.785714285714286, 1, 0.777777777777778, 1, 1, 0.5, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.818181818181818, 1, 0.954545454545455, 
    0.6, 0.714285714285714, 1, 1, 0.571428571428571, 0.666666666666667, 
    1, 0.611111111111111, 0.961538461538462, 0.777777777777778, 
    1, 1, 1, 1, 1, 1, 1, 1, 0.5, 0.846153846153846, 1, 0.75, 
    1, 1, 1, 1, 0.625, 0.866666666666667, 0.769230769230769, 
    0.85, 22, 27, 24, 17, 1, 4, 11, 5, 15, 7, 22, 13, 22, 9, 
    14, 3, 32, 59, 46, 39, 10, 14, 1, 2, 2, 1, 6, 8, 3, 7, 1, 
    6, 2, 6, 2, 10, 19, 20, 53, 43, 11, 14, 1, 3, 7, 12, 10, 
    7, 7, 3, 7, 6, 13, 5, 16, 29, 53, 35, 44, 37, 2, 2, 3, 3, 
    1, 9, 8, 4, 4, 6, 9, 5, 14, 6, 10, 29, 43, 60, 61, 12, 2, 
    1, 3, 3, 4, 11, 7, 7, 5, 6, 1, 1, 6, 19, 21, 11, 29, 48, 
    18, 15, 7, 1, 6, 9, 11, 21, 12, 10, 3, 7, 4, 2, 10, 11, 25, 
    14, 16, 45, 23, 6, 6, 7, 2, 7, 5, 11, 15, 15, 7, 9, 9, 22, 
    10, 13, 10, 17), .Dim = c(136L, 3L), .Dimnames = list(NULL, 
        c("most", "perc", "n")))), .Names = c("hod", "dow", "gid"
), row.names = c(NA, -136L), class = "data.frame")
grssnbchr
  • 2,877
  • 7
  • 37
  • 71
  • It seems a bit tricky with several scales for one aesthetic in ggplot. See e.g. [this answer and the comments](http://stackoverflow.com/questions/11752709/using-two-scale-colour-gradients-on-one-ggplot/11752870#11752870). Well, not much of a help to you, but it could at least be good to know that, at least to my knowledge, there is no simple function to solve this. – Henrik Oct 25 '13 at 08:44

1 Answers1

1

You don't have commensurate scales for gid.perc and gid.n. Values for gid.n range between 1 and 61 and values for gid.perc are between 0 and 1. So when you default assign them to the same linear scale for alpha all of the percent values are basically at the so transparent they are not visible end of the alpha scale.

I can't reproduce your exact graphic as you have several functions not available in base R - but here is a quick way to make a more appropriate graphic. I just transform gid.n to have the same 0-1 range and use that new variable as the alpha aesthetic.

d$nS <- (d$gid[,'n'] - min(d$gid[,'n']))/(max(d$gid[,'n']) - min(d$gid[,'n']))
p2 <- ggplot(d,aes(dow,hod)) +
  geom_tile(aes(fill = as.factor(gid[,'most']), alpha = gid[,'perc'])) +
  geom_point(aes(alpha = nS)) 
p2

enter image description here

Andy W
  • 5,031
  • 3
  • 25
  • 51