简体   繁体   中英

R plotly: preserving appearance of TWO legends when converting ggplot2 with ggplotly

I have come to notice that when converting a ggplot2 plot to an interactive plotly plot using the ggplotly function, strange things may occur.

I am plotting a "Punchcard plot", a nice way to present 4 dimensions of a data set:

df <- data.frame(cat1 = rep(c("a","b","c"), 3), cat2 = c(rep("A", 3),
    rep("B", 3), rep("C", 3)), var1 = 1:9, var2 = 10:18)
ggplot(df, aes(x=cat1, y=cat2, size= var1, fill = var2)) +
    geom_point(shape=21)

在此处输入图片说明

However, when I use ggplotly to convert to interactive, plotly only presents one of the legends:

p <- ggplot(df, aes(x=cat1, y=cat2, size= var1, fill = var2)) +
    geom_point(shape=21)
ggplotly(p)

在此处输入图片说明

  1. Why does plotly do this, how can I avoid this behavior?

  2. Seeing that I encounter more and more of these oddities - anyone has a link to somewhere I can read on how ggplotly works, in a way I can fix these issues myself in the future?

The second legend gets lost in during the conversion (or at least I couldn't find in the data). You can look at the result of ggplotly and modify everything from the raw data to the layout, eg gp[['x']][['layout']] would contain all the layout variables passed from ggplotly .


在此处输入图片说明

A lot more lines of code but you have full control over all aspects of your graph.

library(plotly)
df <- data.frame(cat1 = rep(c("a","b","c"), 3), 
                 cat2 = c(rep("A", 3),
                          rep("B", 3), 
                          rep("C", 3)), 
                 var1 = 1:9, 
                 var2 = 10:18)

size_multi <- 2 #multiplies your size to avoid pixel sized objects
color_scale <- list(c(0, "#000000"), list(1, "#00BFFF")) 
p <- plot_ly(df, 
              type='scatter', 
              mode='markers', 
              x = ~cat1, 
              y = ~cat2, 
              marker = list(color = ~var2, 
                            size=~var1 * size_multi,
                            colorscale = color_scale,
                            colorbar = list(len = 0.8, y = 0.3),
                            line = list(color = ~var2,
                                        colorscale = color_scale,
                                        width = 2)
                            ), 
              showlegend = F)



#adds some dummy traces for the punch card markers
markers = c(min(df$var1), mean(df$var1), max(df$var1))
for (i in 1:3) {
  p <- add_trace(p, 
                 df, 
                 type = 'scatter', 
                 mode = 'markers', 
                 showlegend = T, 
                 name = markers[[i]], 
                 x = 'x', 
                 y = 'x', 
                 marker = list(size = markers[[i]] * size_multi, 
                               color='rgba(255,255,255,0)',
                               showscale = F,
                               line = list(color = 'rgba(0,0,0,1)',
                                           width = 2))
                 )
}

#fix the coordinate system
spacer <- 0.2
p <- layout(p, xaxis=list(range=c(-spacer, length(levels(df$cat1)) - 1 + spacer)), yaxis=list(range=c(-spacer, length(levels(df$cat1)) - 1 + spacer)))
p

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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