sha*_*ker 4 graphics r ggplot2
我做了一个这样的图:
library(reshape2)
library(ggplot2)
m <- matrix(1:64 - 32, 8)
rownames(m) <- colnames(m) <-
c(paste0("a", 1:3), paste0("b", 1:2), paste0("c", 1:3))
d <- melt(m)
gg <- ggplot(d) +
geom_tile(aes(x = Var1, y = Var2, fill = value)) +
scale_fill_gradient2()
Run Code Online (Sandbox Code Playgroud)
如何以编程方式在"a","b"和"c"组周围绘制框?
矩阵m
将始终是正方形.colnames(m)
并且rownames(m)
将永远是相同的.因此,盒子将覆盖整个网格,并且永远不会重叠.一般而言,群体规模会有所不同.
我也没有结婚ggplot2
.image
如果它不比ggplot2
/ grid
版本更繁琐,我对基本图形的解决方案持开放态度.
我得到了
d$group <- substr(d$Var1, 1, 1)
Run Code Online (Sandbox Code Playgroud)
在我意识到我不知道如何继续之前.
是)我有的:
我想要的是:
要么 geom_segment
library('reshape2')
library('ggplot2')
m <- matrix(1:64 - 32, 8)
rownames(m) <- colnames(m) <-
c(paste0("a", 1:3), paste0("b", 1:2), paste0("c", 1:3))
gg <- ggplot(melt(m)) +
geom_tile(aes(x = Var1, y = Var2, fill = value)) +
scale_fill_gradient2()
tt <- table(gsub('\\d+', '', colnames(m)))
ll <- unname(c(0, cumsum(tt)) + .5)
gg + geom_segment(aes(x = ll, xend = ll, y = head(ll, 1), yend = tail(ll, 1))) +
geom_segment(aes(y = ll, yend = ll, x = head(ll, 1), xend = tail(ll, 1)))
Run Code Online (Sandbox Code Playgroud)
这可能被认为是作弊,但很简单:
# Depending on your data you may be able to generate `d2` directly
# here we need to re-order a bit
d2 <- transform(
d, V1 = substr(Var1, 1, 1),
V2=factor(substr(Var2, 1, 1), levels=letters[3:1])
)
ggplot(d2) +
geom_tile(aes(x = Var1, y = Var2, fill = value)) +
scale_fill_gradient2() +
facet_grid(V2 ~ V1, scales="free", space="free")
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
523 次 |
最近记录: |