我正在尝试在 R 中制作如下图(在 MATLAB 中生成)。
我需要使用 ggplot 因为我需要将矩形一个一个地添加到“网格”中,并根据强度从 0 到 1 的矩阵设置每个矩形的背景。现在,我有一个包含所有矩形(密度属性)和矩形边框(顶部、左侧、右侧、底部、top.lefts 等)的强度。可以在此处找到我的完整 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))