简体   繁体   English

R情节图例过滤

[英]R Plotly legend filtering

I have copypasted the code example from plotly website ( https://plot.ly/ggplot2/geom_bar ), but the graph behave differently from the example when filtered; 我已经从plotly网站( https://plot.ly/ggplot2/geom_bar )复制了该代码示例,但是该图形的行为与过滤后的示例有所不同。 how can I have mine to be able to reshape like that? 我怎样才能重塑?

code: 码:

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)

From the web: 通过网络: 阴谋网站
My RStudio Viewer: 我的RStudio查看器:

R Studio查看器

If you compare the data produced by the example code and the published code closely, you can see two differences. 如果将示例代码和已发布的代码生成的数据进行紧密比较,则会发现两个区别。

  1. The "wrong" graph has base values “错误”图具有base
  2. The order is reversed between the two graphs 两个图之间的顺序相反

In order to prevent stacked bar graphs from not collapsing if you toggle a trace, you would need to erase the base values (and for aesthetic reasons reverse the order of the traces). 为了防止在切换轨迹时堆叠的条形图不塌陷,您需要擦除base值(并且出于美学原因,请反转轨迹的顺序)。

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")

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

p <- ggplotly(gp)

for (i in 1:length(p$x$data)) {
  p$x$data[[i]]$base <- c()
  tmp <- p$x$data[[i]]
  p$x$data[[i]] <- p$x$data[[length(p$x$data) - i + 1]]
  p$x$data[[length(p$x$data) - i + 1]] <- tmp
}
p

在此处输入图片说明

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

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