ggplot 图例选项不会在闪亮的 ggplotly 中采用

ggplot legend options will not be adopted in ggplotly in shiny

我用 ggplot2 创建了一个图,想用 plotly 在我的 Shiny App 中发布它。不幸的是,图例的位置没有被采纳。此外,图例中的每个条目都添加了 1,绘图的标题消失了。

我的部分情节

plot = ggplot() +
  geom_bar(data = pi_plot, aes(x = as.Date(Month), y = value, fill = Domain), position = "stack", stat = "identity") +
  geom_line(data = pi_fc_ly_plot, aes(x = as.Date(Month), y = value, group = PI, colour = PI), size = 0.75) +
  theme(legend.title = element_blank()) +
  labs(title = "Traffic",
       subtitle = "Visits", 
       y = "Pageviews", 
       x = "Month", 
       caption = "Source: Google Analytics") +
  scale_x_date(labels = date_format("%B"), 
               date_breaks = "month",
               expand = c(0, 0)) +
  scale_y_continuous(legend.position = "bottom") 

在应用中调用剧情

ggplotly(plot)

我前段时间做过类似的事情。我将我的图例位置指定为我的一部分 ggplotly 函数调用而不是我的 ggplot 函数调用。您可以尝试修改您的代码如下,看看是否有效

plot = ggplot() +
  geom_bar(data = pi_plot, aes(x = as.Date(Month), y = value, fill = Domain), position = "stack", stat = "identity") +
  geom_line(data = pi_fc_ly_plot, aes(x = as.Date(Month), y = value, group = PI, colour = PI), size = 0.75) +
  theme(legend.title = element_blank()) +
  labs(title = "Traffic",
       subtitle = "Visits", 
       y = "Pageviews", 
       x = "Month", 
       caption = "Source: Google Analytics") +
  scale_x_date(labels = date_format("%B"), 
               date_breaks = "month",
               expand = c(0, 0))

 ggplotly(plot) %>% layout(legend = list(orientation = "h", y=-0.5))

我不知道你的字幕问题的来源。