r - ggplotly on geom_bar and coord_flip seems to fail
问题描述
I am able to plot a geom_bar as I want but when I try to wrap it with the ggplotly
function it does not work. Anyone know a trick to make it work?
library(ggplot2)
library(plotly)
# My df dataset
a=runif(10, min = 0, max = 1)
b=runif(10, min = -1, max = 0)
df=data.frame("p"=c(a,b),
"g"=rep(c("a","b"),each=10),
"type"=c(letters[1:10],letters[1:10]))
# Build the g plot
g <- ggplot(df,
aes(x = as.factor(type), y = p, fill=g)) +
geom_bar(data=df[df$g == "a",] ,stat = "identity") +
geom_bar( data=df[df$g == "b",] ,stat = "identity") +
coord_flip() +
scale_y_continuous(breaks = seq(-1, 1, 0.25),
labels = paste0(abs(seq(-1, 1, 0.25)))) +
xlab("") +
theme_bw()
# plot it the regular way
g
# plot it the ggplotly way
ggplotly(g)
解决方案
The issue might be with splitting up the two geom_bar
calls--you don't actually need to do two of these.
Also note that geom_col()
does the same as geom_bar(stat = "identity")
.
Try this:
g <- df %>%
ggplot(aes(x = type, y = p, fill = g)) +
geom_col() +
coord_flip()
ggplotly(g)
Add back in your scaling, etc--I just dropped everything else for simplicity.
推荐阅读
- firebase - 即使在登录后,firebase 中的权限也丢失或不足
- javascript - Wordpress:仅在使用短代码的页面上加载 CSS 和 JS
- python - 将日期与两个长度不等的数据框中的日期范围进行比较
- javascript - POST 请求保持等待状态,但从未给出响应
- node.js - 有没有办法为 NodeJs 异步执行“线程”获取某种“上下文”?
- algorithm - O(|V| * k) 是否等于 O(|E|)?
- c++ - 如何为未排序的分区搜索中断递归
- spring - 如何在spring控制器中使用websocketSendMessage
- python - 如果python中的元素相同,如何交换列表中的元素?
- trello - 从卡的详细信息中添加成员?