2017-07-26 3 views
2

ggplot2とplotlyの問題に直面しています。 ggplot2を使って棒グラフを作成し、それを関数ggplotlyに渡すと、変数の選択を解除するときにバーが空中に表示されます。グラフは例として振る舞いませんhere変数を選択解除したときのプロット*フライ*の棒グラフ

例:

library(ggplot2) 
library(reshape2) 
library(plotly) 

df1 <- data.frame("Price" = rnorm(3, mean = 100, sd = 4), 
       "Type" = paste("Type", 1:3)) 

df2 <- data.frame("Price" = rnorm(3, mean = 500, sd = 4), 
        "Type" = paste("Type", 1:3)) 

df <- rbind(df1, df2) 

df$Dates <- rep(c("2017-01-01", "2017-06-30"), 3) 

df <- melt(df, measure.vars = 3) 

p <- ggplot(df, aes(fill=Type, y=Price, x=value)) + 
    geom_bar(stat="identity", position = "stack") 

ggplotly(p) 

G1

enter image description here

Imは、以下で実行されている:

> sessionInfo() 
R version 3.3.2 (2016-10-31) 
Platform: x86_64-w64-mingw32/x64 (64-bit) 
Running under: Windows >= 8 x64 (build 9200) 

locale: 
[1] LC_COLLATE=Swedish_Sweden.1252 LC_CTYPE=Swedish_Sweden.1252 LC_MONETARY=Swedish_Sweden.1252 LC_NUMERIC=C     
[5] LC_TIME=Swedish_Sweden.1252  

attached base packages: 
[1] stats  graphics grDevices utils  datasets methods base  

other attached packages: 
[1] zoo_1.8-0   dygraphs_1.1.1.4 plotly_4.7.0.9000 reshape2_1.4.2  ggplot2_2.2.1.9000 lubridate_1.6.0 readxl_1.0.0 

ありがとう!

答えて

1

私は問題がggplot2とプロット間の相互作用にあると思います。 直接plot_ly関数を使用

p <- plot_ly(df, x = ~value, y = ~Price, type = 'bar',split=~Type) %>% 
    layout(yaxis = list(title = 'Count'), barmode = 'stack') 
p 
+0

ありがとう、それはそうです。私はggplot2でプロットを作成することを好むが、あなたのソリューションは私の問題を解決した。 – Pierre

関連する問題