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

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

问题描述

我注意到当使用 ggplotly 函数将 ggplot2 绘图转换为交互式 plotly 绘图时,

但是,当我使用 ggplotly 转换为交互式时,plotly 只呈现其中一个图例:

p <- ggplot(df, aes(x=cat1, y=cat2, size= var1, fill = var2)) +几何点(形状=21)ggplotly(p)

  1. 为什么plotly会这样做,我该如何避免这种行为?

  2. 看到我遇到越来越多的这些怪事 - 任何人都有指向我可以阅读的地方的链接,了解 ggplotly 的工作原理,这样我将来可以自己解决这些问题?

解决方案

第二个图例在转换过程中丢失了(或者至少我在数据中找不到).您可以查看 ggplotly 的结果并修改从原始数据到布局的所有内容,例如gp[['x']][['layout']] 将包含从 ggplotly 传递的所有布局变量.

<小时>

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

图书馆(情节)df <- data.frame(cat1 = rep(c("a","b","c"), 3),cat2 = c(rep("A", 3),代表(B",3),代表(C",3)),var1 = 1:9,var2 = 10:18)size_multi <- 2 #乘以你的大小以避免像素大小的对象color_scale <- list(c(0, "#000000"), list(1, "#00BFFF"))p <- plot_ly(df,类型='分散',模式='标记',x = ~cat1,y = ~cat2,标记=列表(颜色=〜var2,size=~var1 * size_multi,色阶 = color_scale,colorbar = list(len = 0.8, y = 0.3),线=列表(颜色=〜var2,色阶 = color_scale,宽度 = 2)),表演传奇 = F)#为打孔卡标记添加一些虚拟痕迹标记 = c(min(df$var1), mean(df$var1), max(df$var1))对于(我在 1:3){p <- add_trace(p,df,类型='分散',模式='标记',秀传奇 = T,名称 = 标记[[i]],x ='x',y = 'x',标记=列表(大小=标记[[i]] * size_multi,颜色='rgba(255,255,255,0)',显示比例 = F,线=列表(颜色='rgba(0,0,0,1)',宽度 = 2)))}#修复坐标系间隔 <- 0.2p <- layout(p, xaxis=list(range=c(-spacer, length(levels(df$cat1)) - 1 + spacer)), yaxis=list(range=c(-spacer, length(levels(df$cat1)) - 1 + 垫片)))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:使用 ggplotly 转换 ggplot2 时保留两个图例的外观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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