这个问题与我之前提出的两个不同的问题有关:
1)重现频率矩阵图
我希望在R中重现这个情节:
我已经做到这一点,使用图形下面的代码:
#Set the number of bets and number of trials and % lines
numbet <- 36
numtri <- 1000
#Fill a matrix where the rows are the cumulative bets and the columns are the trials
xcum <- matrix(NA, nrow=numbet, ncol=numtri)
for (i in 1:numtri) {
x <- sample(c(0,1), numbet, prob=c(5/6,1/6), replace = TRUE)
xcum[,i] <- cumsum(x)/(1:numbet)
}
#Plot the trials as transparent lines so you can see the build up
matplot(xcum, type="l", xlab="Number of …Run Code Online (Sandbox Code Playgroud) 我在文本文件(data.csv)中有(x,y)数据我想制作热图.
X Y
-60 -18
60 -62
7 14
-22 -60
59 58
29 22
-58 -18
60 -61
7 14
-21 -59
61 59
29 22
-57 -18
-22 -59
59 60
29 24
-56 -17
61 -60
8 16
-20 -58
62 60
30 23
Run Code Online (Sandbox Code Playgroud)
理想情况下,我希望能够导入文本文件并将其保存为450px x 200px的图像文件(PNG或JPG).
热图需要更像您在Google地图上找到的(例如此处),而不是矩阵(此处为示例).
提前致谢.
我在一年前问了这个问题并得到了这个"概率热图"的代码:

numbet <- 32
numtri <- 1e5
prob=5/6
#Fill a matrix
xcum <- matrix(NA, nrow=numtri, ncol=numbet+1)
for (i in 1:numtri) {
x <- sample(c(0,1), numbet, prob=c(prob, 1-prob), replace = TRUE)
xcum[i, ] <- c(i, cumsum(x)/cumsum(1:numbet))
}
colnames(xcum) <- c("trial", paste("bet", 1:numbet, sep=""))
mxcum <- reshape(data.frame(xcum), varying=1+1:numbet,
idvar="trial", v.names="outcome", direction="long", timevar="bet")
library(plyr)
mxcum2 <- ddply(mxcum, .(bet, outcome), nrow)
mxcum3 <- ddply(mxcum2, .(bet), summarize,
ymin=c(0, head(seq_along(V1)/length(V1), -1)),
ymax=seq_along(V1)/length(V1),
fill=(V1/sum(V1)))
head(mxcum3)
library(ggplot2)
p <- ggplot(mxcum3, aes(xmin=bet-0.5, xmax=bet+0.5, ymin=ymin, ymax=ymax)) +
geom_rect(aes(fill=fill), …Run Code Online (Sandbox Code Playgroud)