几何图形栏:颜色渐变和交叉图案填充(使用gridSVG),透明度问题

几何图形栏:颜色渐变和交叉图案填充(使用gridSVG),透明度问题,r,ggplot2,R,Ggplot2,使用awesomeggplot包,我想要一个条形图,其中fill美学被映射到一个连续变量,实际上是qvalues,在它上面是一个纹理,实际上是条纹和交叉图案填充 颜色渐变很重要,因为它代表意义,而纹理则显示类别“A”、“B”及其重叠。因此,对角线在一个方向,相反的方向和交叉图案填充分别。 我知道维恩图可以完成这项工作,但我们有35个样本,我想比较起来会更容易看到 填充很简单,但是纹理填充很复杂。因此,多亏了(参见和),我成功地获得了带有虚拟数据的输出: 问题在于十字架背景的透明度。如果可能的话

使用awesome
ggplot
包,我想要一个条形图,其中
fill
美学被映射到一个连续变量,实际上是qvalues,在它上面是一个纹理,实际上是条纹和交叉图案填充

颜色渐变很重要,因为它代表意义,而纹理则显示类别“A”、“B”及其重叠。因此,对角线在一个方向,相反的方向和交叉图案填充分别。 我知道维恩图可以完成这项工作,但我们有35个样本,我想比较起来会更容易看到

填充
很简单,但是纹理填充很复杂。因此,多亏了(参见和),我成功地获得了带有虚拟数据的输出:

问题在于十字架背景的透明度。如果可能的话,让
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"))

我在回答我自己的问题,因为有一种方法可以填充图案的特定颜色,多亏了这一点。 在第一个栏上,对于“A”类,它可以如下所示:

pat1
模式替换为以下代码:

pat1 <- pattern(gTree(children=gList(
                      rectGrob(gp=gpar(col=NA, fill=cols[4])),
                      linesGrob(gp=gpar(col="black", lwd = 5)))),
                      width = unit(5, "mm"), height = unit(5, "mm"),
                      dev.width = 1, dev.height = 1)

pat1这并不是一个真正的答案,但我将提供以下代码作为参考,供希望了解我们如何完成此任务的人参考。一个实时版本是。我几乎认为完全使用
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"
      )
    )
  )
)
库(“ggplot2”)
库(“gridSVG”)
图书馆(“gridExtra”)
图书馆(“dplyr”)
图书馆(“RColorBrewer”)

dfso我不知道答案,但是如果我们从SVG的角度来看,我们可能会得到一些帮助。你看到了吗?今天我将试着玩一玩,看看我能发现什么。@timelyportfolio谢谢!我不知道。我绝对不想坚持使用gridSVG,不管怎样,这个项目似乎不太活跃。最好切换到另一种方法来应用纹理。您打算如何使用此方法?最终的输出是什么?实际上我们在这里发布它,一方面使用填充,另一方面使用illustrator中叠加的图案。很遗憾,但至少你能看出我们是多么想这么做。再次感谢您的帮助谢谢@timelyportfolio!它确实有效,我接受了答案,因为这是我们现在最好的答案
svglite
+
纹理
覆盖得很好。当然可以改进,但已经很好了。我想知道它的伸缩性有多好。此外,它假设数据保持其原始顺序。无论如何,它不会用于大量条,也不会可读。我之前的尝试也希望得到同样的订单。最好是使用
ggplot
扩展,将纹理映射到变量,但这超出了我的技能范围。
pat1 <- pattern(gTree(children=gList(
                      rectGrob(gp=gpar(col=NA, fill=cols[4])),
                      linesGrob(gp=gpar(col="black", lwd = 5)))),
                      width = unit(5, "mm"), height = unit(5, "mm"),
                      dev.width = 1, dev.height = 1)
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"
      )
    )
  )
)