[英]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)
If you compare the data produced by the example code and the published code closely, you can see two differences. 如果将示例代码和已发布的代码生成的数据进行紧密比较,则会发现两个区别。
base
values base
值 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.