简体   繁体   English

R Shiny ggiraph和d3heatmap兼容性问题

[英]R Shiny ggiraph and d3heatmap Compatibility Issues

I'm trying to add an interactive heatmap to my Shiny app, but I also have interactive graphs using ggiraph. 我正在尝试向我的Shiny应用程序添加交互式热图,但是我也使用ggiraph生成了交互式图形。 I'm currently using the d3heatmap package, but the heatmaps don't render in the app. 我目前正在使用d3heatmap软件包,但热图未在应用程序中呈现。 I've created a toy example to illustrate this: 我创建了一个玩具示例来说明这一点:

library(shiny)
library(ggiraph)
library(d3heatmap)

ui <- fluidPage(
    d3heatmapOutput('d3'),
    ggiraphOutput('gg')
)

server <- function(input, output, session) {

    # Create heatmap
    output$d3 <- renderD3heatmap({
        d3heatmap(matrix(1:100, nrow = 100, ncol = 100))
    })

    # Create ggiraph
    output$gg <- renderggiraph({
        p <- ggplot(iris, aes(x = Sepal.Length, y = Petal.Width,
                              color = Species, tooltip = iris$Species) ) +
             geom_point_interactive()

        ggiraph(code = {print(p)})
    })
}

shinyApp(ui =  ui, server = server)

Together, only the ggiraph renders, but the heatmap does not. 在一起,仅怪兽渲染,而热图则不渲染。 However, if you comment out the ggiraph code, the heatmap renders. 但是,如果注释掉ggiraph代码,则会显示热图。 I tried switching the order of loading the packages, but that still didn't work. 我尝试切换加载程序包的顺序,但是仍然没有用。

I'm currently running on R 3.2.2 (I have to use this version because the company servers only run on this version, and neither my manager nor I have the authority to update it). 我当前在R 3.2.2上运行(我必须使用此版本,因为公司服务器仅在此版本上运行,并且我的经理和我都没有权限对其进行更新)。 I tried downloading the shinyheatmap, heatmaply, and heatmap.2 packages, but because of versioning issues, the installations were unsuccessful. 我尝试下载Shinyheatmap,heatmaply和heatmap.2软件包,但由于版本问题,安装未成功。

So right now, I've just used pheatmap to create the heatmaps, but they aren't interactive (ie, I can't get values when I hover over individual cells, and I can't zoom in). 因此,现在,我仅使用了pheatmap来创建热图,但是它们并不具有交互性(即,当我将鼠标悬停在单个单元格上时无法获取值,并且无法放大)。 Is there any workaround for this, or are there other interactive heatmap packages out there that would work? 是否有任何解决方法,或者还有其他可行的交互式热图软件包? I'd like to avoid changing all of my ggiraph graphs to plotly graphs since there are a lot of them in my code. 我想避免将我的所有ggiraph图更改为绘图图,因为我的代码中有很多图。

Please let me know if there's any other information you need. 如果您需要其他任何信息,请告诉我。 Any suggestions would be much appreciated! 我们欢迎所有的建议!

(just to let you know I am the author of ggiraph) There is a conflict between ggiraph and d3heatmap because ggiraph is using d3.js version 4 and d3heatmap is using D3.js version 3 . (只是让您知道我是ggiraph的作者)ggiraph和d3heatmap之间存在冲突,因为ggiraph使用的是d3.js版本4,而d3heatmap使用的是D3.js版本3 I don't think there is a solution to solve that conflict. 我认为没有解决该冲突的解决方案。

However, building an interactive heatmap with ggplot2/ggiraph is not that difficult. 但是,使用ggplot2 / ggiraph构建交互式热图并不是那么困难。 See below: 见下文:

library(dplyr)
library(tidyr)
library(ggplot2)
library(ggiraph)
library(ggdendro)


# mydata <- cor(mtcars)
mydata <- matrix(runif(2500, min = -2, max = 2), ncol = 50)
row.names(mydata) <- paste0("row_", seq_len(nrow(mydata)))
colnames(mydata) <- paste0("col_", seq_len(ncol(mydata)))

# dendrogram for rows
hc <- hclust(dist(mydata), "ave")
dhr <- as.dendrogram(hc)
order_r <- rownames(mydata)[hc$order]

# dendrogram for columns
hc <- hclust(dist(t(mydata)), "ave")
dhc <- as.dendrogram(hc)
order_c <- colnames(mydata)[hc$order]

# the data
expr_set <- bind_cols(
  data_frame(rowvar = rownames(mydata)),
  as.data.frame(mydata)
)
expr_set <- gather(expr_set, colvar, measure, -rowvar)
expr_set$rowvar <- factor( expr_set$rowvar, levels = order_r )
expr_set$colvar <- factor( expr_set$colvar, levels = order_c )
expr_set <- arrange(expr_set, rowvar, colvar)

# get data for dendrograms - IMHO, ggdendro is the hero here...
data_c <- dendro_data(dhc, type = "rectangle")
data_c <- segment(data_c) %>% mutate(
  y = y + length(order_r) + .5,
  yend = yend + length(order_r) + .5
)

data_r <- dendro_data(dhr, type = "rectangle")
data_r <- segment(data_r)
data_r <- data_r %>%
  mutate( x_ = y + length(order_c) + .5,
          xend_ = yend + length(order_c) + .5,
          y_ = x,
          yend_ = xend )

expr_set <- expr_set %>% 
  mutate( 
    tooltip = sprintf("Row: %s<br/>Col: %s<br/>measure: %.02f", 
                      rowvar, colvar, measure) ,
    data_id = sprintf("%s_%s", rowvar, colvar)
    )


# all data are tidy and can be now used with ggplot
p <- ggplot(data = expr_set, aes(x = colvar, y = rowvar) ) +
  geom_tile_interactive(aes(fill = measure, tooltip = tooltip, data_id = data_id), colour = "white") +
  scale_fill_gradient(low = "white", high = "#BC120A") +
  geom_segment(
    data = data_c,
    mapping = aes(x = x, y = yend, xend = xend, yend = y),
    colour = "gray20", size = .2) +
  geom_segment(
    data = data_r,
    mapping = aes(x = x_, y = y_, xend = xend_, yend = yend_),
    colour = "gray20", size = .2) +
  coord_equal()

# cosmetics
p <- p + theme_minimal() +
  theme(
    legend.position = "right",
    panel.grid.minor = element_line(color = "transparent"),
    panel.grid.major = element_line(color = "transparent"),
    axis.ticks.length   = unit(2, units = "mm"),
    plot.title = element_text(face = "bold", hjust = 0.5, size = 12),
    axis.title = element_text(size = 9, colour = "gray30"),
    axis.text.y = element_text(hjust = 1, size = 5, colour = "gray40"),
    axis.text.x = element_text(angle = 90, hjust = 1, size = 5, colour = "gray40"),
    legend.title=element_text(face = "bold", hjust = 0.5, size=8),
    legend.text=element_text(size=6)
  )



ggiraph(ggobj = p)

在此处输入图片说明

Hope it helps 希望能帮助到你

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

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