R ggplot2每个美学的多个比例/图例,重新访问
我有一个例子,我想用ggplot突出显示序列比对的几个属性。我正在使用geom_瓷砖,希望有两套不同颜色的瓷砖用于20个属性。我只能想象一个 我意识到每个美学的一个尺度()的局限性,但也许有人知道如何在这样的情况下破解它,在一个“情节”中使用不同的颜色尺度是有意义的 也许手动添加Grob,但我不知道从哪里开始 另外一个问题:出于某种原因,R ggplot2每个美学的多个比例/图例,重新访问,r,ggplot2,data-visualization,R,Ggplot2,Data Visualization,我有一个例子,我想用ggplot突出显示序列比对的几个属性。我正在使用geom_瓷砖,希望有两套不同颜色的瓷砖用于20个属性。我只能想象一个 我意识到每个美学的一个尺度()的局限性,但也许有人知道如何在这样的情况下破解它,在一个“情节”中使用不同的颜色尺度是有意义的 也许手动添加Grob,但我不知道从哪里开始 另外一个问题:出于某种原因,override.aes=list(shape=“A”)不起作用,你知道为什么吗 还有一个问题:有没有任何方法可以根据瓷砖的大小按比例缩放文本(或者相反) 我会
override.aes=list(shape=“A”)
不起作用,你知道为什么吗
还有一个问题:有没有任何方法可以根据瓷砖的大小按比例缩放文本(或者相反)
我会使用文本大小来表示分数2:
ggplot(pd[pd$score1 != 0,], aes(x=x, y=species)) +
coord_fixed(ratio = 1.5, xlim=c(0.5,16.5), ylim=c(0.5, 3.5)) +
geom_tile(aes(fill=score1)) +
scale_fill_gradient2("Score 1", limits=c(0,4),low="#762A83", mid="white", high="#1B7837", guide=guide_colorbar(title.position="top")) +
geom_text(data=pd, aes(label=letters, size = score2, color=factor(change)), family="mono") +
scale_size_continuous(range = c(4, 8)) +
scale_color_manual("Change", values=c("black", "#F2A11F"), labels=c("None", "Some"), guide=guide_legend(direction="vertical", title.position="top", override.aes=list(shape = "A"))) +
theme(panel.background=element_rect(fill="white", colour="white"),
axis.title = element_blank(),
axis.ticks.y = element_blank(),
axis.text.y = element_text(family="mono", size=rel(2)),
axis.text.x = element_text(size=rel(0.7)),
legend.text = element_text(size=rel(0.7)),
legend.key.size = unit(0.7, "lines"),
legend.position = "bottom", legend.box = "horizontal") +
ggtitle("What about Score2?")
更新:
这里是一个快速的黑客,我不确定这是不是很容易检查视觉虽然
library(ggplot2)
library(grid)
library(proto)
GeomTile2 <- proto(ggplot2:::GeomTile, {
reparameterise <- function(., df, params) {
df <- .$.super$reparameterise(df, params)
if (params$ud == "u")
transform(df, ymin = y)
else
transform(df, ymax = (y-ymin)*0.8 + ymin, ymin = (y-ymin)*0.2 + ymin)
}
draw <- function(..., ud) {.$.super$draw(..., ud)}
})
geom_tile2 <- function (mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., ud = "u") {
GeomTile2$new(mapping = mapping, data = data, stat = stat, position = position, ..., ud = ud)
}
ggplot(pd, aes(x=x, y=species)) +
coord_fixed(ratio = 1.5, xlim=c(0.5,16.5), ylim=c(0.5, 3.5)) +
geom_tile2(aes(fill=score1), ud = "u") +
geom_tile2(aes(fill = score2), ud = "d") +
scale_fill_gradient2("Score 1", limits=c(0,4),low="#762A83", mid="white", high="#1B7837", guide=guide_colorbar(title.position="top")) +
geom_text(data=pd, aes(label=letters, color=factor(change)), size=rel(5), family="mono") +
scale_color_manual("Change", values=c("black", "#F2A11F"), labels=c("None", "Some"), guide=guide_legend(direction="vertical", title.position="top", override.aes=list(shape = "A"))) +
theme(panel.background=element_rect(fill="white", colour="white"),
axis.title = element_blank(),
axis.ticks.y = element_blank(),
axis.text.y = element_text(family="mono", size=rel(2)),
axis.text.x = element_text(size=rel(0.7)),
legend.text = element_text(size=rel(0.7)),
legend.key.size = unit(0.7, "lines"),
legend.position = "bottom", legend.box = "horizontal") +
ggtitle("What about Score2?")
库(ggplot2)
图书馆(网格)
图书馆(原型)
GeomTile2I通过将两个单独生成的图中的GROB组合起来,获得了令人满意的结果。我相信这个解决方案可以更好地推广,以适应不同的grob指数
library(ggplot2)
library(grid)
pd = data.frame(
letters = strsplit("AGTGACCGACTATCATAGTGACCCAGAATCATAGTGACCGAGTATGAT", "")[[1]],
species = rep(c("Human", "Armadillo", "Porcupine"), each=16),
x = rep(1:16, 3),
change = c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,
0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0),
score1 = c(0,0,0,0,0,0,1,1,2,2,2,3,3,3,4,3,
0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
score2 = c(0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,
0,0,0,0,2,2,2,2,0,0,0,0,0,0,0,0,
0,0,0,0,3,3,3,3,0,0,0,0,0,0,0,0)
)
p1=ggplot(pd[pd$score1 != 0,], aes(x=x, y=species)) +
coord_fixed(ratio = 1.5, xlim=c(0.5,16.5), ylim=c(0.5, 3.5)) +
geom_tile(aes(fill=score1)) +
scale_fill_gradient2("Score 1", limits=c(0,4),low="#762A83", mid="white", high="#1B7837", guide=guide_colorbar(title.position="top")) +
geom_text(data=pd, aes(label=letters, color=factor(change)), size=rel(5), family="mono") +
scale_color_manual("Change", values=c("black", "#F2A11F"), labels=c("None", "Some"), guide=guide_legend(direction="vertical", title.position="top", override.aes=list(shape = "A"))) +
theme(panel.background=element_rect(fill="white", colour="white"),
axis.title = element_blank(),
axis.ticks.y = element_blank(),
axis.text.y = element_text(family="mono", size=rel(2)),
axis.text.x = element_text(size=rel(0.7)),
legend.text = element_text(size=rel(0.7)),
legend.key.size = unit(0.7, "lines"),
legend.position = "bottom", legend.box = "horizontal") +
ggtitle("Voila, the Score2!")
p2=ggplot(pd[pd$score2 != 0,], aes(x=x, y=species)) +
coord_fixed(ratio = 1.5, xlim=c(0.5,16.5), ylim=c(0.5, 3.5)) +
geom_tile(aes(fill=score2)) +
scale_fill_gradient2("Score 2", limits=c(0,3),low="#1B7837", mid="white", high="#762A83", guide=guide_colorbar(title.position="top")) +
geom_text(data=pd, aes(label=letters, color=factor(change)), size=rel(5), family="mono") +
scale_color_manual("Change", values=c("black", "#F2A11F"), labels=c("None", "Some"), guide=guide_legend(direction="vertical", title.position="top", override.aes=list(shape = "A"))) +
theme(panel.background=element_rect(fill="white", colour="white"),
axis.title = element_blank(),
axis.ticks.y = element_blank(),
axis.text.y = element_text(family="mono", size=rel(2)),
axis.text.x = element_text(size=rel(0.7)),
legend.text = element_text(size=rel(0.7)),
legend.key.size = unit(0.7, "lines"),
legend.position = "bottom", legend.box = "horizontal") +
ggtitle("What about Score2?")
p1g=ggplotGrob(p1)
p2g=ggplotGrob(p2)
combo.grob = p1g
combo.grob$grobs[[8]] = cbind(p1g$grobs[[8]][,1:4],
p2g$grobs[[8]][,3:5],
size="first")
combo.grob$grobs[[4]] = reorderGrob(
addGrob(p1g$grobs[[4]],
getGrob(p2g$grobs[[4]],
"geom_rect.rect",
grep=TRUE)),
c(1,2,5,3,4))
grid.newpage()
grid.draw(combo.grob)
谢谢koshke,我已经考虑过了,但这并不是一个足够的视觉线索来表示这个属性。这些排列的长度可以是100个或更多的字符,并且可以包含几十个物种,因此字母相当小,通常没有空间来控制它们的大小。此外,有时分数是一个连续变量,很难区分从一个序列到另一个序列的相对数量。恐怕我需要一些更“直观”的东西来举一个更真实的例子:@Krizbi Updated please find.Hi@kohske谢谢你的努力,但我担心它在视觉上不能代表足够清晰的信息。我需要一些互补的颜色,因为它是一个完全不同的属性。此外,图例还可以很快告诉观察者一个图形中有多少不同的属性。因此,比例是关键,而不是几何图形。我能想到的最好的方法是使用alpha通道并将其映射到线型,但这有一个缺点,即必须处理单个连续颜色渐变。也许可以建议@hadley考虑一下:“一个aes-一个尺度”规则的折衷方案是只允许注释几何具有无限尺度和美学的组合。然后,注释将尽可能“丰富多彩”,但不会干扰实际的(非常合理的)规则,并对尽可能多的注释层具有自己的比例。这样,用户可以更自由地对绘图进行注释,并保留ggplot2的所有优秀功能和视觉一致性。只是一个想法。。。
library(ggplot2)
library(grid)
pd = data.frame(
letters = strsplit("AGTGACCGACTATCATAGTGACCCAGAATCATAGTGACCGAGTATGAT", "")[[1]],
species = rep(c("Human", "Armadillo", "Porcupine"), each=16),
x = rep(1:16, 3),
change = c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,
0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0),
score1 = c(0,0,0,0,0,0,1,1,2,2,2,3,3,3,4,3,
0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
score2 = c(0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,
0,0,0,0,2,2,2,2,0,0,0,0,0,0,0,0,
0,0,0,0,3,3,3,3,0,0,0,0,0,0,0,0)
)
p1=ggplot(pd[pd$score1 != 0,], aes(x=x, y=species)) +
coord_fixed(ratio = 1.5, xlim=c(0.5,16.5), ylim=c(0.5, 3.5)) +
geom_tile(aes(fill=score1)) +
scale_fill_gradient2("Score 1", limits=c(0,4),low="#762A83", mid="white", high="#1B7837", guide=guide_colorbar(title.position="top")) +
geom_text(data=pd, aes(label=letters, color=factor(change)), size=rel(5), family="mono") +
scale_color_manual("Change", values=c("black", "#F2A11F"), labels=c("None", "Some"), guide=guide_legend(direction="vertical", title.position="top", override.aes=list(shape = "A"))) +
theme(panel.background=element_rect(fill="white", colour="white"),
axis.title = element_blank(),
axis.ticks.y = element_blank(),
axis.text.y = element_text(family="mono", size=rel(2)),
axis.text.x = element_text(size=rel(0.7)),
legend.text = element_text(size=rel(0.7)),
legend.key.size = unit(0.7, "lines"),
legend.position = "bottom", legend.box = "horizontal") +
ggtitle("Voila, the Score2!")
p2=ggplot(pd[pd$score2 != 0,], aes(x=x, y=species)) +
coord_fixed(ratio = 1.5, xlim=c(0.5,16.5), ylim=c(0.5, 3.5)) +
geom_tile(aes(fill=score2)) +
scale_fill_gradient2("Score 2", limits=c(0,3),low="#1B7837", mid="white", high="#762A83", guide=guide_colorbar(title.position="top")) +
geom_text(data=pd, aes(label=letters, color=factor(change)), size=rel(5), family="mono") +
scale_color_manual("Change", values=c("black", "#F2A11F"), labels=c("None", "Some"), guide=guide_legend(direction="vertical", title.position="top", override.aes=list(shape = "A"))) +
theme(panel.background=element_rect(fill="white", colour="white"),
axis.title = element_blank(),
axis.ticks.y = element_blank(),
axis.text.y = element_text(family="mono", size=rel(2)),
axis.text.x = element_text(size=rel(0.7)),
legend.text = element_text(size=rel(0.7)),
legend.key.size = unit(0.7, "lines"),
legend.position = "bottom", legend.box = "horizontal") +
ggtitle("What about Score2?")
p1g=ggplotGrob(p1)
p2g=ggplotGrob(p2)
combo.grob = p1g
combo.grob$grobs[[8]] = cbind(p1g$grobs[[8]][,1:4],
p2g$grobs[[8]][,3:5],
size="first")
combo.grob$grobs[[4]] = reorderGrob(
addGrob(p1g$grobs[[4]],
getGrob(p2g$grobs[[4]],
"geom_rect.rect",
grep=TRUE)),
c(1,2,5,3,4))
grid.newpage()
grid.draw(combo.grob)