2017-01-22 51 views
0

我碰到了我認爲是R的plotly包中的ggplotly函數的一個怪癖。與geom_bar和ggplotly怪癖

當試圖在ggplotly函數中包含ggplot圖(使用geom_barstat = identity)時,負值被強制轉換爲正值。

下面是一個玩具例如:

library(ggplot2) 
library(plotly) 

set.seed(12345) 

x <- data.frame(
    x = 1:10, 
    obs = floor(rnorm(10) * 100) 
) 

# x obs 
# 1 58 
# 2 70 
# 3 -11 
# 4 -46 
# 5 60 
# 6 -182 
# 7 63 
# 8 -28 
# 9 -29 
# 10 -92 

test_plot <- ggplot(x, aes(factor(x), obs)) + geom_bar(stat = "identity") 
test_plot 

enter image description here

ggplotly(test_plot) 

enter image description here

的值似乎沒有使用其它geoms時被強制。我錯過了什麼嗎?

感謝您的幫助。

+1

https://github.com/ropensci/plotly/issues/560 – MLavoie

回答

1

你可以試試這個:

library(plotly) 
set.seed(12345) 

df <- data.frame(
    x = as.factor(1:10), 
    obs = floor(rnorm(10) * 100) 
) 
plot_ly(x = df$x, y = df$obs, type = 'bar', name = 'Plotly') %>% 
    layout(xaxis = list(title = 'x'), yaxis = list(title = 'obs'), barmode = 'group') 

enter image description here

+0

感謝提供這種解決方法Sandipan! –

+0

非常歡迎 –