问题描述
我有一个例子,我想用ggplot来强调序列比对的几个属性。我正在使用geom_tile,并希望为两个乐谱属性分配两组不同颜色的磁贴。我只能看到一个。
我意识到每种美学的一个缩放限制(),但也许有人有一个想法如何在这样的情况下对它进行破解,在一个'情节'中有不同的色阶。
也许手动添加Grob,但我不知道从哪里开始......
an额外的问题:出于某种原因, override.aes = list(shape =A)
不起作用,有什么想法为什么?
再多一个:按比例缩放文本大小的方法(或其他方式)?
library(ggplot2)
library(grid)
pd = data.frame(
letters = strsplit(AGTGACCGACTATCATAGTGACCCAGAATCATAGTGACCGAGTATGAT,
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)
)
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 ,color =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 =单元(0.7,lines),
legend.position =bottom,legend.box =horizontal)+
ggtitle(Score2?怎么样? )
我通过组合来自两个单独生成的图表的grobs,获得了令人满意的结果。我相信这个解决方案可以更好地适应不同的grob指数...
library(ggplot2)
库(网格)
pd = data.frame(
letters = strsplit(AGTGACCGACTATCATAGTGACCCAGAATCATAGTGACCGAGTATGAT,)[[1]],
species = rep(c(人类,犰狳,豪猪),每个= 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,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),
得分2 = 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,0,3,3,3 ,3,0,0,0,0,0,0,0,0)
)
p1 = ggplot(pd [pd $ score1!= 0,] ,aes(x = x,y =物种))+
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 .position =top,override.aes = list(shape =A)))+
theme(panel.background = element_rect(fill =white,color =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,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 = 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,color =white),
axis.title = element_blank(),
axis.ticks.y = element_blank(),
axis.text.y = element_text(family =mono,size = rel(2)),
轴。 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,lege nd.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)
I have an example where I'd like to highlight several properties of sequence alignments using ggplot. I'm using geom_tile and want to have two sets of differently coloured tiles for two score properties. I am only able to visualize one.
I am aware of the limitation of one scale per aesthetic (and the logic behind it), but maybe someone has an idea how to hack it for cases like this where it would make sense to have different colour scales in one 'plot'.
Perhaps with adding manually the Grobs, but I wouldn't know where to start...
an additional question: for some reason the override.aes=list(shape = "A")
does not work, any ideas why?
one more: any method to scale text proportionally to the size of the tile (or the other way around)?
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)
)
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("What about Score2?")
I managed to get a satisfactory result by combining grobs from two separately generated plots. I'm sure the solution can be generalized better to accommodate different grob indices ...
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)
这篇关于ggplot2每个审美的多重比例/传说,重新审视的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!