简体   繁体   English

使用ggplotly转换Alpha美学

[英]Convert alpha aesthetics with ggplotly

I have been working on heatmaps using in ggplot2 , and I am now trying to convert them to ggplotly to build a shiny app with them. 我一直在使用ggplot2使用热图,现在我正尝试将它们转换为ggplotly以使用它们构建shiny应用程序。 Everything works (more or less) fine except for the translation of the alpha aesthetic in geom_tile (same issue if I use geom_raster ). 除了geom_tile中的alpha美学翻译(如果我use geom_rastergeom_tile同样的问题)之外,其他所有内容都可以正常运行( use geom_raster )。 After a full day of googling and reading related answers, I have not found any solution yet. 经过一整天的搜索和阅读相关答案之后,我还没有找到任何解决方案。

I have tried with different data and specifications, and so far no luck at all. 我尝试使用不同的数据和规格,到目前为止根本没有运气。 The problem seems to be specific to heatmaps, as alpha translates well if I use geom_point. 这个问题似乎特定于热图,因为如果我使用geom_point,alpha转换效果很好。 Problem is, I really need heatmaps for what I'm doing. 问题是,我确实需要进行工作的热图。

I have produced a reproducible example that shows the issue. 我制作了一个可重现的示例来说明问题。

library(ggplot2)
library(plotly)
library(dplyr)

sample<- data.frame(a = 1:10,
                b = 11:20,
                c = 31:40,
                d = rep(c("a", "b"), 5))


plot <- sample%>%
  ggplot(aes(x=a, y=b, fill = c, alpha = d))+
  geom_raster()

plot

ggplotly(plot)

You'll see the output is quite different. 您会看到输出完全不同。 Any ideas or has anyone had a similar issue? 有什么想法或有类似的问题吗?

ggplot: ggplot:

ggplot

ggplotly: ggplotly:

ggplotly

I agree there's something not being translated as intended by plotly::ggplotly() . 我同意plotly::ggplotly()并未按预期翻译某些内容。 I tried a few variations of ggplot2::geom_rect() & ggplot2::geom_tile() and ggplot2::scale_color_manual() & ggplot2::scale_color_identity() . 我尝试了ggplot2::geom_rect()ggplot2::geom_tile()ggplot2::scale_color_manual()ggplot2::scale_color_identity()

Here's a heatmap example written straight to plotly, without the ggplot2 layer. 这是不带ggplot2层而直接编写为散点图的热图示例。 I'm not seeing a directly way to map alpha/opacity. 我没有看到直接映射Alpha /不透明度的方法。

This should meet your needs if you didn't need to vary the alpha in plotly, and simply needed the geom dimensions to be correct. 如果您不需要在绘图中更改Alpha,而仅需要几何尺寸正确,这应该可以满足您的需求。

ds <- tibble::tibble(
  x   =  1:10,
  y   = 11:20,
  z   = 31:40,
  a1  = rep(c("a", "b"), 5),
  a2  = rep(c( .1,   .4), 5)
)

plot_ly(ds) %>% 
  add_heatmap(x = ~x, y = ~y, z = ~z, opacity = .5,  colors = "YlGnBu")

绘图热图

If you need to vary alpha, I'm guessing the best current approach is to create a custom palette that sets all four channels of rgb a . 如果您需要改变alpha值,我猜当前最好的方法是创建一个自定义调色板,该调色板设置rgb a的所有四个通道。

In case anyone runs into a similar issue, I found a way around this (as it seems to be a bug of ggplotly) by creating the heatmap with geom_point() and adjusting the shape and size of the points until it looked the same. 万一有人遇到类似的问题,我找到了解决方法(这似乎是ggplotly的错误),方法是使用geom_point()创建热图,并调整点的形状和大小,直到看起来相同为止。 Apparently, ggplotly does translate well the alpha aes from this geom. 显然,ggplotly确实很好地转换了该几何图形的alpha aes。

This is the final result (the aim of the shiny app is to be able to select these diagonals with a reference-year to isolate them from the others): 这是最终结果(闪亮的应用程序的目的是能够选择一个具有参考年份的对角线以将其与其他对角线隔离):

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM