简体   繁体   English

单击图例时,ggplot堆积的条形图消失

[英]Plotly ggplot stacked bar chart disappear when legend is clicked

I'm not sure why but my stacked bar chart disappears instead of falling to the axis when the legend is clicked. 我不确定为什么,但是单击图例后,堆积的条形图消失了,而不是掉到了轴上。 I've attached a screenshot of the example that I copied from the plotly website and the code are as follows: 我已经附上了我从pallyly网站复制的示例的屏幕截图,代码如下:

library(plotly)

DF <- read.table(text="Rank F1     F2     F3
1    500    250    50
2    400    100    30
3    300    155    100
4    200    90     10", header=TRUE)

library(reshape2)
DF1 <- melt(DF, id.var="Rank")

p <- ggplot(DF1, aes(x = Rank, y = value, fill = variable)) +
  geom_bar(stat = "identity")

p <- ggplotly(p)

Stacked Bar Chart disappears when legend is clicked 单击图例时,堆积的条形图消失

Can anyone assist me with this? 有人可以协助我吗?

You can use the plotly API directly, rather than ggplotly, then it works as expected: 您可以直接使用plotly API,而不是ggplotly,然后按预期工作:

plot_ly(DF1) %>%
  add_bars(~Rank, ~value, color=~variable) %>%
  layout(barmode = 'stack')

If you need to also embed a static version of the plot in an R Markdonw document, you can use the export() function to create a static version: 如果您还需要在R Markdonw文档中嵌入图的静态版本,则可以使用export()函数创建静态版本:

---
title: "Untitled"
output: word_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## R Markdown

Here's a barchart:

```{r chart}
library(plotly)
library(reshape2)

DF <- read.table(text="Rank F1     F2     F3
                 1    500    250    50
                 2    400    100    30
                 3    300    155    100
                 4    200    90     10", header=TRUE)
DF1 <- melt(DF, id.var="Rank")

p = plot_ly(DF1) %>%
  add_bars(~Rank, ~value, color=~variable) %>%
  layout(barmode = 'stack')
export(p)
```

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

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