R plotly:使用ggplotly()时如何保持ggplot2 geom_bar()的宽度参数

时间:2021-10-29 13:26:37

For some reason, when converting in R a ggplot2 bar chart (geom_bar) into an interactive plot using plotly's ggplotly function, ggplotly "forces" the bars to stick together, even if the width parameter is specified:

出于某种原因,当使用plotly的ggplotly函数将R ggplot2条形图(geom_bar)转换为交互式图时,即使指定了width参数,ggplotly也会“强制”将条粘在一起。

library(plotly)
library(ggplot2)

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

ggplotly(p)

R plotly:使用ggplotly()时如何保持ggplot2 geom_bar()的宽度参数

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

ggplotly(p)

R plotly:使用ggplotly()时如何保持ggplot2 geom_bar()的宽度参数

It also reverses ggplot2s default colors order for some reason (although keeping the order in the legend...), but I can handle this.

由于某种原因,它也会颠倒ggplot2s的默认颜色顺序(尽管在图例中保持顺序......),但我可以处理这个问题。

Any idea hot to tell ggplotly to not stick my bars together, like the default ggplot2 behavior?

有什么好主意告诉ggplotly不要把我的酒吧粘在一起,比如默认的ggplot2行为?

1 个解决方案

#1


2  

Any idea hot to tell ggplotly to not stick my bars together, like the default ggplot2 behavior?

有什么好主意告诉ggplotly不要把我的酒吧粘在一起,比如默认的ggplot2行为?

ggplotly sets bargap to 0, you could set to your desired value via layout

ggplotly将bargap设置为0,您可以通过布局设置为所需的值

ggplotly(p) %>% layout(bargap=0.15)

R plotly:使用ggplotly()时如何保持ggplot2 geom_bar()的宽度参数


It also reverses ggplot2s default colors order for some reason (although keeping the order in the legend...), but I can handle this.

由于某种原因,它也会颠倒ggplot2s的默认颜色顺序(尽管在图例中保持顺序......),但我可以处理这个问题。

Let's get that fixed as well. You can change the order afterwards by flipping the bars and reversing the legend.

我们也可以解决这个问题。之后您可以通过翻转条形并反转图例来更改顺序。

gp <- ggplotly(p) %>% layout(bargap = 0.15, legend = list(traceorder = 'reversed'))

traces <- length(gp[['x']][[1]])
for (i in 1:floor(traces / 2)) {
  temp <- gp[['x']][[1]][[i]]
  gp[['x']][[1]][[i]] <- gp[['x']][[1]][[traces + 1 - i]]
  gp[['x']][[1]][[traces + 1 - i]] <- temp
}

R plotly:使用ggplotly()时如何保持ggplot2 geom_bar()的宽度参数

#1


2  

Any idea hot to tell ggplotly to not stick my bars together, like the default ggplot2 behavior?

有什么好主意告诉ggplotly不要把我的酒吧粘在一起,比如默认的ggplot2行为?

ggplotly sets bargap to 0, you could set to your desired value via layout

ggplotly将bargap设置为0,您可以通过布局设置为所需的值

ggplotly(p) %>% layout(bargap=0.15)

R plotly:使用ggplotly()时如何保持ggplot2 geom_bar()的宽度参数


It also reverses ggplot2s default colors order for some reason (although keeping the order in the legend...), but I can handle this.

由于某种原因,它也会颠倒ggplot2s的默认颜色顺序(尽管在图例中保持顺序......),但我可以处理这个问题。

Let's get that fixed as well. You can change the order afterwards by flipping the bars and reversing the legend.

我们也可以解决这个问题。之后您可以通过翻转条形并反转图例来更改顺序。

gp <- ggplotly(p) %>% layout(bargap = 0.15, legend = list(traceorder = 'reversed'))

traces <- length(gp[['x']][[1]])
for (i in 1:floor(traces / 2)) {
  temp <- gp[['x']][[1]][[i]]
  gp[['x']][[1]][[i]] <- gp[['x']][[1]][[traces + 1 - i]]
  gp[['x']][[1]][[traces + 1 - i]] <- temp
}

R plotly:使用ggplotly()时如何保持ggplot2 geom_bar()的宽度参数