使用超棒的ggplot
程序包,我想要一个将fill
美学映射到一个连续变量(实际上是q值)并在其上贴上一个纹理(实际上是条纹和交叉影线)的条形图。
颜色渐变非常重要,因为它代表了重要性,而纹理将显示类别“ A”,“ B”及其重叠。因此,对角线采用一种方式,分别是相反的方式和交叉影线。我知道维恩图可以完成这项工作,但是我们认为有35个样本,比较起来比较容易理解。
它fill
是微不足道的,但是纹理之一是棘手的。在SO上,感谢@baptise(请参阅此处和此处),我设法获得了包含伪数据的输出:
问题是十字架背景的透明度。如果可能的话,可以alpha
在grid.patternFill()
函数中在后台进行工作。不幸的是,在我手中,它不起作用。
任何帮助都感激不尽。
虚拟数据可以在这里加载:
dfso <- structure(list(Sample = c("S1", "S2", "S1", "S2", "S1", "S2"),
qvalue = c(14.704287341, 8.1682824035, 13.5471896224, 6.71158432425,
12.3900919038, 5.254886245), type = structure(c(1L, 1L, 2L,
2L, 3L, 3L), .Label = c("A", "overlap", "B"), class = "factor"),
value = c(897L, 1082L, 503L, 219L, 388L, 165L)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -6L), .Names = c("Sample",
"qvalue", "type", "value"))
代码在这里:
library("ggplot2")
library("gridSVG")
library("gridExtra")
library("dplyr")
library("RColorBrewer")
cols <- brewer.pal(7,"YlOrRd")
pso <- ggplot(dfso)+
geom_bar(aes(x = Sample, y = value, fill = qvalue, linetype = type), width = .8, colour = "black", stat = "identity", position = "stack", alpha = 1)+
theme_classic(18)+
theme( panel.grid.major = element_line(colour = "grey80"),
panel.grid.major.x = element_blank(),
panel.grid.minor = element_blank(),
legend.key = element_blank(),
axis.text.x = element_text(angle = 90, vjust = 0.5))+
guides(linetype = FALSE) +
ylab("Count")+
scale_fill_gradientn("-log10(qvalue)", colours = cols, limits = c(0, 20))+
scale_linetype_manual(values = c("dotted", "solid", "dotted"))+
scale_y_continuous(expand = c(0, 0), limits = c(0, 2000))
# gridSVG
pat1 <- pattern(linesGrob(gp = gpar(col="black", lwd = 1)),
width = unit(5, "mm"), height = unit(5, "mm"),
dev.width = 1, dev.height = 1)
pat2 <- pattern(linesGrob(x = unit(0:1, "npc"), y = unit(1:0, "npc"),
gp = gpar(col="black", lwd = 1)),
width = unit(5, "mm"), height = unit(5, "mm"),
dev.width = 1, dev.height = 1)
crossGrob <- gTree(children = gList(linesGrob(gp = gpar(col="black", lwd = 1)), linesGrob(x = unit(0:1, "npc"), y = unit(1:0, "npc"), gp = gpar(col="black", lwd = 1))))
registerPatternFill("hash1", pat1)
registerPatternFill("hash2", pat2)
registerPatternFill("cross", grob = crossGrob, dev.width = 1, dev.height = 1, width = unit(5, "mm"), height = unit(5, "mm"))
gridsvg("crossbars.svg", width = 10)
print(pso)
grid.force()
grid.patternFill("geom_rect.rect", alpha = 0.2, grep = TRUE, group = FALSE,
label = rep(c("hash1", "cross", "hash2"), 1))
dev.off()
在grid.patternFill
该alpha
参数是想给透明度,据我的理解它。但是,它没有效果,并且颜色丢失。我只在第一个柱上填充了图案,所以您可以看到对比度。
编辑:的alpha
工作正常,但它作用于图案本身,即线条。这就解释了为什么线条看起来如此苍白。问题更多是背景被假定为白色且没有透明度。
该linetype
映射是突出的重叠部分的尝试,但它是不是很好。如果透明度适用,gridSVG
我将放弃此部分并始终保持实线。
提前谢谢了,
奥雷利安
如有使用,输出sessionInfo()
:
R version 3.2.1 (2015-06-18)
Platform: x86_64-apple-darwin14.3.0 (64-bit)
Running under: OS X 10.10.4 (Yosemite)
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] grid stats graphics grDevices utils datasets methods base
other attached packages:
[1] gridExtra_0.9.1 RColorBrewer_1.1-2 dplyr_0.4.1 gridSVG_1.4-3 ggplot2_1.0.1
loaded via a namespace (and not attached):
[1] Rcpp_0.11.6 XML_3.98-1.3 assertthat_0.1 digest_0.6.8 MASS_7.3-42 plyr_1.8.3 DBI_0.3.1
[8] gtable_0.1.2 magrittr_1.5 scales_0.2.5 stringi_0.5-5 reshape2_1.4.1 labeling_0.3 proto_0.3-10
[15] RJSONIO_1.3-0 tools_3.2.1 stringr_1.0.0 munsell_0.4.2 parallel_3.2.1 colorspace_1.2-6
这并不是一个真正的答案,但是我将提供以下代码,以作为可能希望了解我们如何完成此任务的人员的参考。现场版在这里。我几乎认为完全使用d3
或建立在上面的库会更容易d3
library("ggplot2")
library("gridSVG")
library("gridExtra")
library("dplyr")
library("RColorBrewer")
dfso <- structure(list(Sample = c("S1", "S2", "S1", "S2", "S1", "S2"),
qvalue = c(14.704287341, 8.1682824035, 13.5471896224, 6.71158432425,
12.3900919038, 5.254886245), type = structure(c(1L, 1L, 2L,
2L, 3L, 3L), .Label = c("A", "overlap", "B"), class = "factor"),
value = c(897L, 1082L, 503L, 219L, 388L, 165L)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -6L), .Names = c("Sample",
"qvalue", "type", "value"))
cols <- brewer.pal(7,"YlOrRd")
pso <- ggplot(dfso)+
geom_bar(aes(x = Sample, y = value, fill = qvalue), width = .8, colour = "black", stat = "identity", position = "stack", alpha = 1)+
ylim(c(0,2000)) +
theme_classic(18)+
theme( panel.grid.major = element_line(colour = "grey80"),
panel.grid.major.x = element_blank(),
panel.grid.minor = element_blank(),
legend.key = element_blank(),
axis.text.x = element_text(angle = 90, vjust = 0.5))+
ylab("Count")+
scale_fill_gradientn("-log10(qvalue)", colours = cols, limits = c(0, 20))
# use svglite and htmltools
library(svglite)
library(htmltools)
# get the svg as tag
pso_svg <- htmlSVG(print(pso),height=10,width = 14)
browsable(
attachDependencies(
tagList(
pso_svg,
tags$script(
sprintf(
"
var data = %s
var svg = d3.select('svg');
svg.select('style').remove();
var bars = svg.selectAll('rect:not(:last-of-type):not(:first-of-type)')
.data(d3.merge(d3.values(d3.nest().key(function(d){return d.Sample}).map(data))))
bars.style('fill',function(d){
var t = textures
.lines()
.background(d3.rgb(d3.select(this).style('fill')).toString());
if(d.type === 'A') t.orientation('2/8');
if(d.type === 'overlap') t.orientation('2/8','6/8');
if(d.type === 'B') t.orientation('6/8');
svg.call(t);
return t.url();
});
"
,
jsonlite::toJSON(dfso)
)
)
),
list(
htmlDependency(
name = "d3",
version = "3.5",
src = c(href = "http://d3js.org"),
script = "d3.v3.min.js"
),
htmlDependency(
name = "textures",
version = "1.0.3",
src = c(href = "https://rawgit.com/riccardoscalco/textures/master/"),
script = "textures.min.js"
)
)
)
)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句