使用ggplotly转换Alpha美学 [英] Convert alpha aesthetics with ggplotly

查看:103
本文介绍了使用ggplotly转换Alpha美学的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用ggplot2中的热图进行处理,现在我正尝试将其转换为ggplotly以使用它们构建shiny应用.除了geom_tile中alpha美学的翻译(如果我use geom_raster时,这是相同的问题)之外,一切都正常(或多或少)正常.经过一整天的搜寻和阅读相关答案之后,我还没有找到任何解决方案.

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. Everything works (more or less) fine except for the translation of the alpha aesthetic in geom_tile (same issue if I use geom_raster). After a full day of googling and reading related answers, I have not found any solution yet.

我尝试使用不同的数据和规格,到目前为止完全没有运气.这个问题似乎特定于热图,因为如果我使用geom_point,alpha转换效果很好.问题是,我确实需要进行工作的热图.

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. 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:

ggplotly:

推荐答案

万一有人遇到类似问题,我通过用并调整点的形状和大小,直到看起来相同为止.显然,ggplotly可以很好地翻译出该几何图形的alpha aes.

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. Apparently, ggplotly does translate well the alpha aes from this geom.

这是最终结果(闪亮的应用程序的目的是能够选择一个具有参考年份的对角线,以将其与其他对角线隔离):

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):

这篇关于使用ggplotly转换Alpha美学的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆