简体   繁体   English

R ggplotly dublicated legend

[英]R ggplotly dublicated legend

I try to generate a plot on which every point stands for an event. 我尝试生成一个情节,每个点代表一个事件。 Color, Size and faced_grid are used to give additional information available in a visual way. Color,Size和faced_grid用于以可视方式提供其他信息。 The graph is working in ggplot2 but it is often important to know the exact numbers so an interactive version is needed which enables to hover over the point and get the info. 该图表在ggplot2中工作,但知道确切的数字通常很重要,因此需要一个交互式版本,可以将鼠标悬停在该点上并获取信息。 I tried to convert the plot into an interactive version with the function ggplotly from the plotly-package. 我尝试使用plotly-package中的函数ggplotly将绘图转换为交互式版本。 The problem then is, that the legend not only display the different states of the used attributes, it contains every existent combination. 问题在于,图例不仅显示已使用属性的不同状态,还包含每个现有组合。 In addition, it did not display info from geom_rect. 此外,它没有显示geom_rect的信息。

I found related/similar questions but they used the function plot_ly and not ggploty or did not provide an answer. 我找到了相关/类似的问题,但是他们使用了plot_ly函数而不是ggploty,或者没有提供答案。

Following, the same problem illustrated with the mtcars dataset: 下面,使用mtcars数据集说明了同样的问题:

library(plotly)

g = ggplot(mtcars,aes(x=mpg,y=disp,color = as.factor(cyl),size =as.factor(gear))) +
    geom_point() +
    geom_text(label = c(rep("A",nrow(mtcars)-5),rep("B",5)),color = "black",size=4) +
    geom_rect(data=data.frame(name="zone",Start=20,End = 30,ymin = -Inf,ymax = Inf),aes(xmin=Start, xmax=End, ymin=ymin, ymax=ymax,fill=name),inherit.aes = FALSE,alpha=0.3)+
    facet_grid(vs~am)
g

This is the result and how it should look like: ggplot Graph 这是结果以及它应该是什么样子: ggplot Graph

Now using ggplotly 现在使用ggplotly

ggplotly(g)

This is the result: ggploty Graph 结果如下: ggploty Graph

(1) The legend is now a combination of the different attributes used for Color and Size (1)图例现在是用于颜色和大小的不同属性的组合

(2) geom_rect is in the legend but didn't get displayed in the graph (2)geom_rect在图例中,但没有显示在图表中

Does anyone knows how to get the same graph in ggplotly like in ggplot2? 有谁知道如何在ggplotly中获得与ggplot2相同的图形? I am grateful for every hint. 我很感激每一个提示。 Thanks 谢谢

Dave 戴夫

I do not know how to fix the combination of legends when you use ggplotly. 当你使用ggplotly时,我不知道如何修复图例的组合。 But, I can fix the second problem, if you do not use the Inf and -Inf, the geom_rect will work: 但是,我可以解决第二个问题,如果你不使用Inf和-Inf,geom_rect将工作:

ggplotly(ggplot(mtcars,aes(x=mpg,y=disp, = as.factor(cyl),size =as.factor(gear))) +
             geom_rect(aes( xmin=20, 
                            xmax=30, 
                            ymin=0, 
                            ymax=max(mtcars$disp),
                            fill="Name"),
                       inherit.aes = FALSE, alpha=0.3) +
             geom_point() +
             geom_text(label = c(rep("A",nrow(mtcars)-5),rep("B",5)), = "black",size=4) +
             facet_grid(vs~am)) 

However, the legends are bad. 但是,传说很糟糕。 I would suggest using subplot to create the same thing in Plotly, and I think this link Ben mentioned will help you create each subplot. 我建议使用subplot在Plotly中创建相同的东西,我认为Ben提到的这个链接将帮助你创建每个子图。 One thing to mention is that I had trouble Illustrating different size in legend in plotly, while the size of the marker will be different, there will not be a legend for the size scale. 有一点需要注意的是,我在绘图中描绘了图例中不同大小的问题,而标记的大小会有所不同,因此不会有大小比例的图例。 Maybe a scale will be a better option. 也许规模将是更好的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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