r - geom_bar : color gradient and cross hatches (using gridSVG), 透明度问题

标签 r ggplot2

使用真棒 ggplot包,我想要一个条形图,其中 fill美学被映射到一个连续变量,实际上是 qvalues 和在它上面的纹理,实际上是条纹和交叉影线。

颜色渐变很重要,因为它代表了重要性,而纹理将显示类别“A”、“B”及其重叠。所以对角线以一种方式,相反的方式和交叉阴影分别。
我知道维恩图可以完成这项工作,但我们有 35 个样本,我认为比较会更容易看到。
fill是微不足道的,但是,质地很棘手。在 SO 上,感谢 @baptise (参见 herehere )我设法用虚拟数据获得了这个输出:
crossbars

问题是 透明度十字架的背景。如果可能,请获取 alphagrid.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

最佳答案

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

更换 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)

对于 geom_bar颜色很少,它会起作用,但对于我的问题,fill颜色映射到热图比例,这将是乏味的。

关于r - geom_bar : color gradient and cross hatches (using gridSVG), 透明度问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31342379/

相关文章:

r - ggmap map 样式存储库?现在 CloudMade 不再提供 API

r - 用r的ggplot2中的线连接点

r - 在图周围裁剪出ggplot2空白

r - 在 R 中推送和弹出列表的更好方法?

r - 如何在 R 中进行数据库连接/查询以进行单元测试

从ggplot2中的小图例框中删除边框和颜色

r - 如何将条形图中的 Y 轴数字更改为百分比?

使用ggplot减少r中X轴上的日期比例

减少 R 中的行数

R 传单 - 如何仅等待 `map_shape_click`