GGPlotly Not Displaying Varying Alpha Values / Alpha Legend

#Hi Everyone,
I’m having an issue with the published ggplot2 graphs not appearing correctly relative to the way they appear in R. I’ve got a geom_tile plot that looks like this:

The uploaded Plotly graph loses all the alpha information from the plot and the legend.

I get an error on converting it using ggplotly() that makes me think it’s only looking for atomic values and not vectors for the alpha channel. Is this the case? Is there any way to remedy this? It seems to currently be only taking the first value The error is…

ppp <- ggplotly(pp, tooltip = c(“y”, “x”, “alpha”, “fill”)) %>%

  • layout(autosize = F, width = 1000, height = 700, font = f, margin = m, xaxis=x, yaxis=y, title = “Plasmid Presence: Confidence, Incompatability Group”)
    Warning messages:
    Warning messages:
    1: In sub("\)", paste0(",", alpha, “)”), x) :
    argument ‘replacement’ has length > 1 and only the first element will be used
    2: In sub("\)", paste0(",", alpha, “)”), x) :
    argument ‘replacement’ has length > 1 and only the first element will be used
    3: In sub("\)", paste0(",", alpha, “)”), x) :
    argument ‘replacement’ has length > 1 and only the first element will be used
    4: In sub("\)", paste0(",", alpha, “)”), x) :
    argument ‘replacement’ has length > 1 and only the first element will be used

My code is…
#Create ggplot
pp <- ggplot(reportable.conf, aes(Plasmid, Sample, alpha = Confidence, fill = Inc_group, text = paste("AMR Gene: ", reportable$AMR_gene))) +
geom_tile(colour = “white”) +
scale_fill_manual(values = colours.inc, name = “Incompatibility Group”) +
theme_classic(base_size = 8) +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.45, size = rel(0.8)),
axis.text.y = element_text(size = rel(0.8)),
axis.title.x=element_blank(),
legend.title = element_text(),
axis.title.y=element_blank(),
panel.background = element_rect(fill=“grey95”)) +
scale_x_discrete(expand = c(0, 0)) +
scale_y_discrete(expand = c(0, 0))

#Some stylish variables
m = list(
l = 150,
r = 100,
b = 200,
t = 100,
pad = 0
)
f <- list(
family = “Arial, Helvetica, sans-serif”,
size = 8,
color = “#7f7f7f
)
x <- list(
title = “Plasmid”,
titlefont = f
)
y <- list(
title = “Sample”,
titlefont = f
)

#Convert to Plotly
ppp <- ggplotly(pp, tooltip = c(“y”, “x”, “alpha”, “fill”)) %>%
layout(autosize = F, width = 1000, height = 700, font = f, margin = m, xaxis=x, yaxis=y, title = “Plasmid Presence: Confidence, Incompatability Group”)

plotly_POST(ppp, filename = “r-docs/PP-test”)

The plotly graph itself
https://plot.ly/~adrian.zetner/11/plasmid-presence-confidence-incompatability-group/

Any ideas?

Hi @zetner, could you also share code/data to generate the reportable.conf object? It might also be helpful to have the output of devtools::session_info() after you’ve created the plot

Hi @carson,

Here is the output from session_info():


R version 3.2.5 (2016-04-14)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 14.04.4 LTS

locale:
 [1] LC_CTYPE=en_CA.UTF-8       LC_NUMERIC=C               LC_TIME=en_CA.UTF-8        LC_COLLATE=en_CA.UTF-8     LC_MONETARY=en_CA.UTF-8    LC_MESSAGES=en_CA.UTF-8   
 [7] LC_PAPER=en_CA.UTF-8       LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_CA.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] gtable_0.2.0       ggdendro_0.1-18    plyr_1.8.3         ggplot2_2.1.0      gridExtra_2.2.1    RGraphics_2.0-14   stringr_1.0.0      RColorBrewer_1.1-2 dendextend_1.1.8  
[10] ape_3.4            dichromat_2.0-0    heatmap.plus_1.3   reshape2_1.4.1     dplyr_0.4.3       

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.4      whisker_0.3-2    magrittr_1.5     MASS_7.3-45      munsell_0.4.3    colorspace_1.2-6 lattice_0.20-33  R6_2.1.2         tools_3.2.5      parallel_3.2.5  
[11] nlme_3.1-127     DBI_0.3.1        digest_0.6.9     lazyeval_0.1.10  assertthat_0.1   labeling_0.3     stringi_1.0-1    scales_0.4.0    

As for the data I’ve uploaded a csv of the data here: https://github.com/TheZetner/plotly-ggplot

The call to ggplot is as follows:

pp <- ggplot(reportable.conf, aes(Plasmid, Sample, alpha = Confidence, fill = Inc_group, text = paste("AMR Gene: ", reportable$AMR_gene))) + 
  geom_tile(colour = "white") + 
  scale_fill_manual(values = colours.inc, name = "Incompatibility Group") +
  theme_classic(base_size = 8) +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.45, size = rel(0.8)),
        axis.text.y = element_text(size = rel(0.8)),
        axis.title.x=element_blank(),
        legend.title = element_text(),
        axis.title.y=element_blank(),
        panel.background = element_rect(fill="grey95")) +
  scale_x_discrete(expand = c(0, 0)) +
  scale_y_discrete(expand = c(0, 0)) 

ppp <- ggplotly(pp, tooltip = c("y", "x", "alpha", "fill")) %>%
layout(autosize = F, width = 1000, height = 700, font = f, margin = m, xaxis=x, yaxis=y, title = "Plasmid Presence: Confidence, Incompatability Group")
plotly_POST(ppp, filename = "r-docs/PP-test")


Thanks, I’ll also need code for creating the colours.inc object

Sorry about that. I should have noticed that needed including…

colours.inc <- colorRampPalette(brewer.pal(length(levels(reportable.conf$Inc_group)), "Paired"))(length(levels(reportable.conf$Inc_group))) # Define as many colours as there are INC levels, Set1 also looks nice