Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在geom_bar和coord_flip上的绘图似乎失败了_R_Ggplot2_Plotly_R Plotly_Ggplotly - Fatal编程技术网

在geom_bar和coord_flip上的绘图似乎失败了

在geom_bar和coord_flip上的绘图似乎失败了,r,ggplot2,plotly,r-plotly,ggplotly,R,Ggplot2,Plotly,R Plotly,Ggplotly,我可以根据需要绘制几何图形条,但当我尝试使用ggplotly函数将其包装时,它不起作用。有人知道如何让它发挥作用吗 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), "typ

我可以根据需要绘制几何图形条,但当我尝试使用
ggplotly
函数将其包装时,它不起作用。有人知道如何让它发挥作用吗

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

问题可能在于拆分两个
geom\u bar
调用——实际上不需要执行其中两个调用

还要注意,
geom\u col()
的作用与
geom\u bar(stat=“identity”)
的作用相同

试试这个:

g <- df %>%
    ggplot(aes(x = type, y = p, fill = g)) +
        geom_col() +
        coord_flip()

ggplotly(g)
g%
ggplot(aes(x=类型,y=p,填充=g))+
geom_col()+
coord_flip()
Ggly(g)

再加上你的缩放,等等——为了简单起见,我放弃了其他所有内容。

这确实更简单,但似乎geom_col还没有像他们说的那样在plolty中实现:geom_GeomCol()还没有在plotly中实现。上面的代码对我来说很有用。我使用的是github版本的ggplot和plotly的v4.7.1使用
plotly\u 4.7.1
为我提供了正确的
plotly
绘图。你有旧版本的吗?
g <- df %>%
    ggplot(aes(x = type, y = p, fill = g)) +
        geom_col() +
        coord_flip()

ggplotly(g)