R plotly:当用gglotlot转换ggplot2时保留两个图例的外观 [英] R plotly: preserving appearance of TWO legends when converting ggplot2 with ggplotly

查看:529
本文介绍了R plotly:当用gglotlot转换ggplot2时保留两个图例的外观的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经注意到,当使用<$ c>将 ggplot2 图转换为交互式绘图图时$ c> ggplotly function,



但是,当我使用 ggplotly 转换为交互式时, plotly 仅显示图例:

  p < -  ggplot(df,aes(x = cat1,y = cat2,size = var1,fill = b 






















更多的代码行,但您可以完全控制图的所有方面。

  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)
$ b $ size_multi< - 2#乘以您的尺寸以避免像素大小的对象
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)



#为打卡标记添加一些虚拟痕迹
markers = c(min(df $ var1),mean(df $ var1),max ($ 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))

}

#修正坐标系
spacer< - 0.2
p < - layout(p,xaxis = list(range = c(-spacer,length(levels(df $ cat1)) - 1 + spacer)),yaxis = list(range = c(-spacer ,长度(等级(df $ cat1)) - 1 + spacer)))
p


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, e.g. 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

这篇关于R plotly:当用gglotlot转换ggplot2时保留两个图例的外观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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