gaospecial / ggVennDiagram

A 'ggplot2' implement of Venn Diagram.
https://gaospecial.github.io/ggVennDiagram/
GNU General Public License v3.0
282 stars 38 forks source link

Colors for plotly figure #26

Closed ivokwee closed 3 years ago

ivokwee commented 3 years ago

How do I change the colors for the plotly figure? I tried the following but doesn't work.

ggVennDiagram(x, show_intersect=TRUE) + scale_fill_gradient(low="grey90",high = "red")

It gives an warning: Ignoring unknown aesthetics: text

gaospecial commented 3 years ago

when show_intersect = TRUE, the function ggVennDiagram() outputs a plotly object, which cannot be further adjusted by scale_fill_gradient().

text is a unofficial aesthetics so there is a warning by default. Normally you can ignore this warning. see ?plotly::ggplotly for more information.

the show_intersect parameter is just a quick and dirty method to let user navigate the region items. If you want more control, you'd better to use stepwise customization of the plot. Please see https://gaospecial.github.io/ggVennDiagram/articles/fully-customed.html.

And the following is an example.

genes <- paste0("gene",1:1000)
set.seed(20210717)
gene_list <- list(A = sample(genes,100),
                  B = sample(genes,200),
                  C = sample(genes,300),
                  D = sample(genes,200))

library(ggVennDiagram)
library(ggplot2)
venn <- Venn(gene_list)
data <- process_data(venn)

# process data manually
items <- venn_region(data) %>%
  dplyr::rowwise() %>%
  dplyr::mutate(text = stringr::str_wrap(paste0(.data$item, collapse = " "),
                                         width = 40)) %>%
  sf::st_as_sf()
label_coord = sf::st_centroid(items$geometry) %>% sf::st_coordinates()

# return a ggplot object
p <- ggplot(items) +
  geom_sf(aes_string(fill="count")) +
  geom_sf_text(aes_string(label = "name"),
               data = data@setLabel,
               inherit.aes = F) +
  geom_text(aes_string(label = "count", text = "text"),
            x = label_coord[,1],
            y = label_coord[,2],
            show.legend = FALSE) +
  theme_void() +
  scale_fill_distiller(palette = "RdBu")
ax <- list(
  showline = FALSE
)

# plot html widget
plotly::ggplotly(p, tooltip = c("text")) %>%
  plotly::layout(xaxis = ax, yaxis = ax)

Please be aware that due to the poor compatibility between plotly and ggplot, this feature is only experimental.