我有这样的数据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
它基本上是一个带有矩阵的 data.frame gid
。hod
是一天中的小时,dow
是上课的一周中的一天Date
(忘记八月,这就是它的变化方式)。
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
这就是它所提供的:
在这种情况下,只有两个不同的离散值,分别用红色和蓝色表示。瓷砖的 alpha 值表示出现这种值的百分比(我也可以将两种颜色混合在一起,但这太复杂了)。
我添加geom_point
是为了也显示gid.n
. 我考虑过设置每个点的 alpha,这样人们就可以识别该值出现了多少次,并通过仅查看点来直观地掌握每个图块的“重要性”。
所以我只改变了:
geom_point(aes(alpha = gid[,'n']))
这似乎根据 正确设置了每个点的 alpha gid.n
,但似乎所有图块都获得相同的低 alpha,而不是基于gid.perc
。
我怎样才能从瓷砖“图形分离”点?
在这里,您可以获得上述示例的全部数据,以防您想重现:
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")