我正在尝试在R中制作如下图像(在MATLAB中生成)
我需要使用ggplot,因为我需要逐个添加矩形到“网格”,并根据强度从0到1的矩阵设置每个矩形的背景。现在,我有一个数据框,矩形(密度属性)和矩形边框(顶部,左边,右边,底部,顶部等)的所有强度。可以在此处找到我的完整R脚本的链接,该脚本定义了下面数据框定义中引用的所有变量(编辑版本):http://pastebin.com/JUUmnTSq。
g <- ggplot()
df <- data.frame(
x = c(1:somPEs),
y = c(1:somPEs),
density = hm2/max(hm2),
tops = fence_edges[,1],
bottoms = fence_edges[,2],
lefts = fence_edges[,3],
rights = fence_edges[,4],
top.lefts = fence_diagonals[,1],
top.rights = fence_diagonals[,2],
bottom.lefts = fence_diagonals[,3],
bottom.rights = fence_diagonals[,4]
)
ggplot() +
geom_rect() + <- the redscaled rectangles
geom_segment() + <-- the grayscaled borders on the rectangles
geom_line() <-- the green scatter plots connected by lines
注意“top.lefts,top.rights”等是对角线“像素”值。从本质上讲,这是神经网络无监督学习的自组织映射的视觉效果。我基本上只是不知道如何使用数据帧信息填写ggplot的geom _ *()方法。
任何人都可以帮我这样做吗? Ggplot非常难以掌握,我无法找到如何使用普通的rect(),plot(),segments()函数等将矩形添加到同一个图中。
感谢您提供的任何帮助!
更新:到目前为止,这是我的进展,基于下面“主题”和“分面”的有用名称删除。基本上我现在想要的是能够根据我指定的强度在边框中着色。有什么想法吗?
# Plot
require(ggplot2)
mxW = max(max(max(w)));
deltax = 1/inputPEs;
hm2 <- as.vector(matrix(hm, byrow=TRUE))
it=1;
# Reshape w to a 3D matrix of dimension: c(sqrt(somPEs), sqrt(somPEs), inputPEs)
dim(w) <- c(sqrt(somPEs), sqrt(somPEs), inputPEs)
w <- aperm(w, c(2, 1, 3))
df <- data.frame(
somPEs = c(1:somPEs),
xp = do.call(rbind, replicate(6, cbind(matrix(0, nrow=sqrt(somPEs)),
t(sapply(rep(1,sqrt(somPEs)), function(i) seq(-1+i+deltax,i-
deltax,deltax)))), simplify=FALSE)),
yp = sapply(c(1:sqrt(somPEs)), function(i) 0.2+0.8*w[i,1:sqrt(somPEs),]/mxW),
density = hm2/max(hm2),
tops = fence_edges[,1],
bottoms = fence_edges[,2],
lefts = fence_edges[,3],
rights = fence_edges[,4],
top.lefts = fence_diagonals[,1],
top.rights = fence_diagonals[,2],
bottom.lefts = fence_diagonals[,3],
bottom.rights = fence_diagonals[,4]
)
ggplot() + geom_rect(data=df,aes(xmin=0,xmax=1,ymin=0,ymax=1,fill=density)) +
geom_line(data=df, aes(x=c(xp.1, xp.2, xp.3, xp.4, xp.5, xp.6),
y=c(yp.1, yp.2, yp.3, yp.4, yp.5, yp.6)), colour="green") +
facet_wrap( ~ somPEs, ncol=sqrt(somPEs)) +
scale_x_continuous(limits = c(0, 1)) + scale_y_continuous(limits = c(0,1))