简体   繁体   English

ggplot在情节缺失的传奇中

[英]ggplot in plotly missing legend

I am having trouble getting a legend to show up in plotly, specifically with ggplot + geom_line . 我无法让一个传奇故事显示出来,特别是ggplot + geom_line

I am using plotly 3.6.0 and ggplot2 2.1.0 我正在使用plotly 3.6.0ggplot2 2.1.0

To demonstrate my problem I'll build a simple data frame: 为了演示我的问题,我将构建一个简单的数据框:

x = 1:5
y1 = 1:5
y2 = y1 * 2
d = data.frame(x, y1, y2)

g = ggplot(data = d, aes(x=x)) +
geom_line(aes(y = y1, col = 'y1')) + 
geom_line(aes(y = y2, col = 'y2')) + 
scale_color_manual(values = c('red', 'blue'), labels = c('y1 lab', 'y2 lab'), name = '')
g

This creates a ggplot figure with a legend. 这会创建一个带有图例的ggplot图形。 However, when I attempt to create an interactive version using plotly the legend is no longer there. 但是,当我尝试使用plotly创建交互式版本时,图例不再存在。

ggplotly(g)

I attempted the following to solve the problem: 我尝试以下方法来解决问题:

g_build = plotly_build(g)
g_build$layout$showlegend <- TRUE
g_build$layout$margin <- list(l=80, r=300, b=80, t=100, pad=0)
g_build
library(reshape2)

d<-melt(d,id="x")
g <- ggplot(data = d, aes(x=x, y=value, color=variable)) +
  geom_line() +scale_color_manual(values = c('red', 'blue'), 
                                  labels = c('y1 lab', 'y2 lab'), name = '')
ggplotly(g)

在此输入图像描述

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

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