使用R中的plot_ly以图形方式单独更改图例

时间:2018-04-23 10:03:14

标签: r plotly

我在protected void Application_Start(object sender, EventArgs e) { Register(GlobalConfiguration.Configuration); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); } 中使用plot_ly,我尝试在下面创建这个简单的图表。我尝试表明观察到2011年之前的年份并预测了年份> = 2011。问题始终是图例

  1. 第一个情节:几乎是我想要的,但是我无法删除跟踪4'传奇条目。
  2. 第二个情节:我无法删除由&#39; <39>
  3. 触发的图例

    有人可以解决这个问题吗?

    R

    谢谢! 曼努埃尔

1 个答案:

答案 0 :(得分:1)

参数showlegend允许禁止图例条目。

# 1st plot
plot_ly() %>% add_trace(data = d1, x=~year, y=~N, color = ~part,
              mode="line", type="scatter") %>% 
add_segments(x = 2010.5, xend = 2010.5, y = 0, yend = max(d1$N), showlegend=FALSE)


# 2nd plot
p <- plot_ly() 

for (k in unique(d1$part)) {
   p <- add_trace(p, data = subset(d1,d1$year <= 2010 & d1$part==k), 
      type="scatter", mode="lines", color=k,
      x=~year, y=~N, legendgroup=k) 
   p <- add_trace(p, data = subset(d1,d1$year > 2010 & d1$part==k), line=list(dash=4),
      type="scatter", mode="lines", color=k,
      x=~year, y=~N,  showlegend=FALSE, legendgroup=k)
}
p

enter image description here enter image description here

相关问题