将变量映射到geom_tile()ru​​gplot中的geom_point()alpha值

时间:2013-10-25 07:42:11

标签: r plot ggplot2

我有这样的数据d

    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

它基本上是一个带有矩阵gid的data.frame。 hod是一天中的小时,dowDate课程中的星期几(忘记八月,这就是它的变换方式)。 gid.most是一个离散值,gid.perc给出了此离散值相对于其他值的百分比(与计算方式无关),gid.n给出了多少次这个确定的离散值出现了。

我想制作一个地毯图,以视觉识别价值观中的某些时间模式。 这就是我所拥有的:

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

这就是它所给出的:

enter image description here

在这种情况下,只有两个不同的离散值,用红色和蓝色表示。 tile的alpha值表示这样一个值出现的百分比(我也可以将两种颜色混合在一起,但这太复杂了)。

我添加了geom_point以便也显示gid.n。我想到了设置每个点的alpha,这样就可以识别出值发生了多少次,并通过仅查看点来直观地掌握每个图块的“重要性”。

所以我只改变了:

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

这似乎是根据gid.n正确设置每个点的alpha, 但似乎所有的瓷砖都是相同的,低α,不是基于gid.perc

enter image description here

我如何从图块中“分割”点?

在这里,您可以获得上述示例的完整数据,以备您重现:

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

1 个答案:

答案 0 :(得分:1)

gid.percgid.n没有相称的比例。 gid.n的值介于1和61之间,gid.perc的值介于0和1之间。因此,当您默认将它们分配给alpha的相同线性比例时,所有百分比值基本上都是如此透明它们不是alpha刻度的可见末端。

我无法重现您的确切图形,因为您有几个基本R中没有的功能 - 但这里有一个快速的方法来制作更合适的图形。我只是将gid.n转换为具有相同的0-1范围,并将该新变量用作alpha美学。

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